Cannot overwrite cookie?

46 views
Skip to first unread message

AM

unread,
Oct 21, 2016, 7:06:06 AM10/21/16
to Fat-Free Framework
I have a cookie playlist, that I want to overwrite

$f3->set("COOKIE.playlist", "0", 60 * 60 * 24 * 30*12*10);

But when I echo it out, it stays the old value:

echo ($f3->get("COOKIE.playlist"));

Any idea why that happens?

ved

unread,
Oct 21, 2016, 8:00:44 AM10/21/16
to Fat-Free Framework
Cookies are set on the client through the use of the Set-Cookie header when the page is loaded so you'll probably only be able to access the changed cookie on the next access.

Please see this SO answer for more details.

Summer White

unread,
Oct 21, 2016, 8:16:21 AM10/21/16
to Fat-Free Framework
I'd expect the same result. If you set a hive value you kind of expect it to echo out what you set.

Does F3 not actually set the value, but instead just set the header? In the code it returns the value. Its the only global key that returns? Why is this i wonder?

            if ($expr[1]=='COOKIE') {
                $parts
=$this->cut($key);
                $jar
=$this->unserialize($this->serialize($this->hive['JAR']));
               
if ($ttl)
                    $jar
['expire']=$time+$ttl;
                call_user_func_array
('setcookie',[$parts[1],$val]+$jar);
                $_COOKIE
[$parts[1]]=$val;
               
return $val;
           
}

I also lean't something new today. That you can follow a switch straight after an else to extend the if statement into a switch statement. Very clever.


                .......
               
if ($ttl)
                    $jar
['expire']=$time+$ttl;
                call_user_func_array
('setcookie',[$parts[1],$val]+$jar);
                $_COOKIE
[$parts[1]]=$val;
               
return $val;
           
}
       
}
       
else switch ($key) {
       
case 'CACHE':
            $val
=Cache::instance()->load($val,TRUE);
           
break;
       
case 'ENCODING':
            ini_set
('default_charset',$val);
           
if (extension_loaded('mbstring'))
                mb_internal_encoding
($val);
           
break;

ved

unread,
Oct 21, 2016, 8:32:28 AM10/21/16
to Fat-Free Framework
Basically it's because that's how cookies work.

F3 could potentially set the value immediately, but then you'd be wrongly assuming that the cookie was set successfully on the client when there's only a guarantee of that after the client receives the set cookie header.

As for the switch after an else, yeah, it looks strange and cool, but you can do that with pretty much anything inside an if/else statement as long as it's just a single instruction, so it's not so different than something like this:

if($something) functionx();
else functionY();
Reply all
Reply to author
Forward
0 new messages