We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Cookie value NULL

Hello,

I found some issues when using phalcon cookies. Il try to make it simple as possible:


            $a = 5;

            if(!$a < 3)
            {
                $setcookie = $this->cookies->set("type:$car", "$brand", time() + 60);
            }

            $getcookie = $this->cookies->get("type:$car");
            var_dump($getcookie);
            die();

For some reason setting cookie inside if statement dosent set value of cookie.

protected '_filter' => null
  protected '_name' => string 'type:sport-car' (length=14)
  protected '_value' => null
  protected '_expire' => int 0
  protected '_path' => string '/' (length=1)
  protected '_domain' => null
  protected '_secure' => null
  protected '_httpOnly' => boolean true

But when I pull out $setcooike from if statement, it works very well:


            $a = 5;

            $setcookie = $this->cookies->set("type:$car", "$brand", time() + 60);

            if(!$a < 3)
            {

            }

            $getcookie = $this->cookies->get("type:$car");
            var_dump($getcookie);
            die();

Vardump:


  protected '_filter' => null
  protected '_name' => string 'type:sport-car' (length=14)
  protected '_value' => string 'BMW' (length=3)
  protected '_expire' => int 1471253476
  protected '_path' => string '/' (length=1)
  protected '_domain' => null
  protected '_secure' => null
  protected '_httpOnly' => boolean true

Does any1 had same problems? Im using PHP 5.6 with Phalcon 2.0.13

edited Aug '16

Well because obviously 3 is not larger than 5 ?



23.6k
edited Aug '16

Well because obviously 3 is not larger than 5 ?

Lol xD .. I just try to made it so simple and miss the I wrote it wrong... No, Thats not a problem m8 .. Il write down real problem example to make it clear ..



23.6k

Here is a real example :

            $brand = "Aston-Martin";

            if (!$this->cookies->has("type:$car")) {
                $setcookie = $this->cookies->set("type:$car", "$brand", time() + 60);

                // some code here that is executed when I dont have cookie setted on

             } else {
                $getcookie = $this->cookies->get("type:$car");

                if ($getcookie->getValue() !== $brand) {
                    $setcookie = $this->cookies->set("type:$car", "$brand", time() + 60);

                    // some code here that is executed when value of cookie is not "Aston-Martin"
                }
            }

In else part, $getcookie value is allways NULL. It set cookie with right name, but value is NULL. And because of that It executing my code in second part of else statement every time.... because $brand is NULL in cookies..

And after refresh there is no cookie or what ? You are sending response after setting those cookies right ?



23.6k
edited Aug '16

And after refresh there is no cookie or what ? You are sending response after setting those cookies right ?

So, when I clean my cookies in browser and open this endpoint :

  1. First part of code is executed because this statemant is true:
    if (!$this->cookies->has("type:$car"))

And it set cookie in my browser with correct cookie name, and in browser it shows me Expires time and value crypted...

  1. Wehn I hit refresh, it will execute second part of else statement because :
    ($getcookie->getValue() !== $brand)

Is allways true. And here is why...

When I vardump this $getcookie in else statement, I get results :

  protected '_filter' => null
  protected '_name' => string 'type:sport-car' (length=14)
  protected '_value' => null
  protected '_expire' => int 0
  protected '_path' => string '/' (length=1)
  protected '_domain' => null
  protected '_secure' => null
  protected '_httpOnly' => boolean true
edited Aug '16

Can't reproduce, my small script on 3.0.0 and apache2:

$di = new Phalcon\DI\FactoryDefault();

$di->set(
    'crypt',
    function () {
        $crypt = new \Phalcon\Crypt();
        $crypt->setKey('[Y9mB{zhq79=~b.N');

        return $crypt;
    }
);

$cookies = $di->get('cookies');
if (!$cookies->has('xyz')) {
    echo 'test';
    $cookies->set('xyz', 123);
}
else {
    echo $cookies->get('xyz')->getValue();
}
/** @var \Phalcon\Http\Response $response */
$response = $di->get('response');
$response->send();

On first load there is test returned, and another loads there is 123 returned.

Post script to reproduce your problem :)



23.6k

Post script to reproduce your problem :)

Something like this:

           $this->di->set('crypt', function () {

                $crypt = new Crypt();

                // Set a global encryption key
                $crypt->setKey('%31.1e$i86e$f!8jz');

                return $crypt;
            }, true);

            $brand = 'Aston-Martin';
            $look = 'sport-car';

            if (!$this->cookies->has("type:$look")) {
                $setcookie = $this->cookies->set("type:$look", $brand, time() + 60);
                echo 'I set the cookie because there is no any'; // because I could not found any
                die();

            } else {
                $getcookie = $this->cookies->get("type:$look");

                if ($getcookie->getValue() !== $brand) {
                    $setcookie = $this->cookies->set("type:$look", $brand, time()+60);
                    echo 'I set the cookie because of brand is not Aston Martin'; // because cookie value is not Aston Martin
                    die();
                }
            }

            echo 'You have Aston Martin value in your cookie!';

            die();
edited Aug '16

I wanted script to reproduce a problem. I don't have $this in single file. Changed to single file:

$di = new Phalcon\DI\FactoryDefault();

$di->setShared('db', function () {
    $connection = new Phalcon\Db\Adapter\Pdo\Mysql([
        'host'     => 'localhost',
        'port'     => 3306,
        'dbname'   => 'phalcon_test',
        'username' => 'root',
    ]);

    return $connection;
});
$di->set('crypt', function () {

    $crypt = new Crypt();

    // Set a global encryption key
    $crypt->setKey('%31.1e$i86e$f!8jz');

    return $crypt;
}, true);

$cookies = $di->get('cookies');
$response = $di->get('response');

$brand = 'Aston-Martin';
$look = 'sport-car';

if (!$cookies->has("type:$look")) {
    $setcookie = $cookies->set("type:$look", $brand, time() + 60);
    echo 'I set the cookie because there is no any'; // because I could not found any
} else {
    $getcookie = $cookies->get("type:$look");

    if ($getcookie->getValue() !== $brand) {
        $setcookie = $cookies->set("type:$look", $brand, time()+60);
        echo 'I set the cookie because of brand is not Aston Martin'; // because cookie value is not Aston Martin
    }
}

$response->send();

Works for me. Looks for problems in your services/controllers or something else, maybe you are not returning response ? Idk there must be somewhere issue, maybe it's your webserver ? As you see it works for me, check my example if it works for you :) It's really simple as hard as it can be.



23.6k
edited Aug '16

Can you please vardump $getcookie in else statement ... That is the part where I get value null ...

    } else {
    $getcookie = $cookies->get("type:$look");
    var_dump($getcookie);
    die();

I get this:

  protected '_name' => string 'sport-car' (length=9)
  protected '_value' => null
  protected '_expire' => int 0
  protected '_path' => string '/' (length=1)
  protected '_domain' => null
  protected '_secure' => null
  protected '_httpOnly' => boolean true


145.0k
Accepted
answer

Actually _value property for me is null too. But when using getValue() it returns correct value.



23.6k

Actually _value property for me is null too. But when using getValue() it returns correct value.

Yeah, it was weried for me to see null in value property.. I also get some aditional characers in my getValue(), but I clean it with trim, so now works everything fine :D

Thank you