note 74589 added to function.setcookie

From: Date: Thu, 19 Apr 2007 01:58:05 +0000
Subject: note 74589 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-125558@lists.php.net to get a copy of this message
Hi, Many peoples have problems with setcookie() on IE... this example was Tested on -------------------------------------- Apache/2.0.52 (CentOS) PHP Version 5.1.6 MS IE 6.x -------------------------------------- Now one of them resolved, i think... I wish share the hack How to fix setcookie() IE bug...? What's the algorhytm...? Example: We have this simple script <?php if ( !isset($_COOKIE['cookyname']) ) { setcookie("cookyname", "value", time()+6600, "/", ".sitename.com"); } else { echo "Created Cookie: ".$_COOKIE['cookyname']; } ?> This cookie will expire in 47 mins. How? In IE we can't create cookies for 1 hours with setcookie() function. There is a bug. We need header() or simple HTTP headers, Javascript, etc. I've test it some different ways. And see the problem, IE ignored time()+3600 expiring value. Until we're setting the value time()+3840 . This means for IE 1 minute expiring value. (and of course, I don't know why). 3840 => 1 min. (tested) 3900 => 2 min. (tested) 3960 => 3 min. (tested) 4020 => 4 min. (tested) 4080 => 5 min. (tested) ........ 6600 => 47 min. ( 6600-3780) / 60 = 47 (tested) and technicaly ; 7140('cause 2 hours(7200) works) - 3780 (IE expire 0 min.) = 3360 3360 / 60 = 56 mins. (not tested) Now we can set the expiring value for IE to 1 to 56 minutes. As You see, we lost the 4 mins. , 'cause we take it from start... 3840-3600=240 && 240 / 60 = 4 mins. And an interesting info: 3780 => means 10 sec. for IE 3600,3660,3720 are all ignored values Conclusion Simple Example : <?php if ( !isset($_COOKIE['cookyname']) ) { // if target browser was Internet Explorer if(stristr($_SERVER['HTTP_USER_AGENT'],"MSIE")) { // SET expiring value to 47 mins. setcookie("cookyname", "alihan", time()+6600, "/", ".sitename.com"); // on Other browsers exp: Firefox } else { setcookie("cookyname", "alihan", time()+2820, "/", ".sitename.com"); } } else { echo "Created Cookie: ".$_COOKIE['cookyname']; } ?> PS: There is no expiring problem with IE on Win32 based Operating Systems... ---- Server IP: 85.111.6.23 Probable Submitter: 81.214.41.107 ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit -- https://master.php.net/note/edit/74589 Del: integrated -- https://master.php.net/note/delete/74589/integrated Del: useless -- https://master.php.net/note/delete/74589/useless Del: bad code -- https://master.php.net/note/delete/74589/bad+code Del: spam -- https://master.php.net/note/delete/74589/spam Del: non-english -- https://master.php.net/note/delete/74589/non-english Del: in docs -- https://master.php.net/note/delete/74589/in+docs Del: other reasons-- https://master.php.net/note/delete/74589 Reject -- https://master.php.net/note/reject/74589 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125558) next »