note 90616 added to function.setcookie

From: Date: Thu, 30 Apr 2009 16:11:51 +0000
Subject: note 90616 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-154051@lists.php.net to get a copy of this message
Be aware of "Last-Modified" header when dealing with cookies. If you are explicitly setting Last-Modified to a past time (a few hours, mins ago, or etc) then even though browser will update your cookie, it won't reflect on your page (if you say <?=$_COOKIE['yourcookie']?> , browser will still print old value). I have seen this in Firefox 3, and in IE8. Here is the code: <?php if(isset($_GET['hide']) && $_GET['hide']=='y'){ setcookie("TmhabarMainNewsHide", 'y', time()+3600*24*1000,"/",".tmhabar.com",0); } elseif(isset($_GET['hide']) && $_GET['hide']=='n'){ setcookie("TmhabarMainNewsHide", 'n' ,time()+3600*24*1000,"/",".tmhabar.com",0); } $last_modified = filemtime('inc/somefile.html'); header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified)." GMT"); echo $_COOKIE['TmhabarMainNewsHide']; ?> Last-Modified header above is being set to a time in the past (last modification time of a certain file). In the browser, after the '/?hide=y' call, output still remains 'n' -> as if cookie value hasn't changed. When you go to Firefox's "View Page Info", there cookie is set to 'y' (which is an expected behavior). Doesn't matter how many times I refreshed the page, or followed other inner links of the page, cookie value never changed. It only changes if you CTRL+F5 in the browser. I guess this is an expected behavior of a browser, not to update the page if it's last-modified header is set to back in time. But on the other hand, dynamic values should change in page. So, just be aware :) Btw, everything worked perfectly once I have commented out the 'header("Last-Modified: "....' line in the code above. Hope this helps someone. Peace. Muhammed Mamedov ---- Server IP: 69.147.83.197 Probable Submitter: 76.193.179.184 ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit -- https://master.php.net/note/edit/90616 Del: integrated -- https://master.php.net/note/delete/90616/integrated Del: useless -- https://master.php.net/note/delete/90616/useless Del: bad code -- https://master.php.net/note/delete/90616/bad+code Del: spam -- https://master.php.net/note/delete/90616/spam Del: non-english -- https://master.php.net/note/delete/90616/non-english Del: in docs -- https://master.php.net/note/delete/90616/in+docs Del: other reasons-- https://master.php.net/note/delete/90616 Reject -- https://master.php.net/note/reject/90616 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154051) next »