siteturbo Posted March 26, 2011 Share Posted March 26, 2011 Hello, I am having trouble setting a cookie with a future expiration. I am using PHP Version 5.1.6. Here is my code: $expire = time() + 60*60*24*30; // 30 days setcookie("TestCookie",$value, $expire); The cookie gets set, however, the cookie is not persistent and expires when I close my browser. This should not be the case but should expire in 30 days. The time() is correct on the server. Here is the raw http headers: Status: HTTP/1.1 200 OK Date: Sat, 26 Mar 2011 15:12:27 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Set-Cookie: TestCookie=my+cookie+value; expires=Mon, 25-Apr-2011 15:12:27 GMT Content-Length: 126 Connection: close Content-Type: text/html; charset=UTF-8 However, when I open Firefox or Chrome they both say that the cookie will expire when I close the browser. I know that my browser is accepting cookies properly because I see other cookies from other websites in the cookie list that expire with future dates. I've tried setting a cookie in the future with javascript and it works fine, like the following: $jsExpire = date("D, j M Y H:i:s e", $expire); echo " <script> <!--// document.cookie = 'jscookie=testcookie; expires=$jsExpire; path=/' //--></script> "; However, when still trying to set the cookie (before headers are sent which is how php operates), php fails to set the cookie properly. Is there any kind of setting in php ini that could effect cookie expiration dates? Obviously it's not a browser problem since I can set cookies correctly with javascript but not php. I even tried sending cookie with header() like this and it doesn't set in the future: header("Set-Cookie: TestCookie=my+cookie+value; expires=Mon, 25-Apr-2011 17:26:45 GMT; path=/; domain=.mydomain.com"); Any help appreciated. Quote Link to comment Share on other sites More sharing options...
sunfighter Posted March 27, 2011 Share Posted March 27, 2011 When you set the cookie are you going to another page? index.php code: <?php $expire = time() + 60*60*24*30; // 30 days setcookie("TestCookie",$value, $expire); header("Location: ./first_page.php"); ?> And then logging out? Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 27, 2011 Author Share Posted March 27, 2011 I'm just staying on the page hitting refresh. The cookie gets set, its just expiring at the end of the session (when I close my browser). Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 Anybody have any suggestions? Still can't figure out why my server won't set a persistent cookie. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 Anybody have any suggestions? Still can't figure out why my server won't set a persistent cookie. Set-Cookie: TestCookie=my+cookie+value; expires=Mon, 25-Apr-2011 15:12:27 GMT well that looks right I think. So it can't be a server problem. Are you sure you don’t have any browser plug-ins to clear cookie on close or anything ? Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 Well, I've tried it in 4 browsers: Firefox, Chrome, Safari, and IE, and they all function the same way. The persistent cookie can not be set using PHP on my server. It works using a javascript cookie set. So, it's not a browser problem. I'm wondering if there is a php cookie problem in v5.1.6 which i'm running. Anybody have any suggestions? Still can't figure out why my server won't set a persistent cookie. Set-Cookie: TestCookie=my+cookie+value; expires=Mon, 25-Apr-2011 15:12:27 GMT well that looks right I think. So it can't be a server problem. Are you sure you don’t have any browser plug-ins to clear cookie on close or anything ? Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 I'm wondering if there is a php cookie problem in v5.1.6 which i'm running. You could find that out by upgrading to 5.3.6 Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 I've actually thought of that. However, I thought I would try the least effort approach first. If push comes to shove, I may have to upgrade. :'( I'm wondering if there is a php cookie problem in v5.1.6 which i'm running. You could find that out by upgrading to 5.3.6 Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 upgrading is like 10 minutes work :? It's worth it for all the new features and performance anyway. Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 It's been a while since I've upgraded like this. I have centos 5.5 and yum installed on the server. Do you have any instructions on how to upgrade? Thanks. upgrading is like 10 minutes work :? It's worth it for all the new features and performance anyway. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 did you install php via yum or compile it from source ? if the first you can just do yum update I think, although my servers use APT so I cant test it. Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 I really don't know. This is a new server that came with PHP already installed. How would I find that out? Thanks. :-) did you install php via yum or compile it from source ? if the first you can just do yum update I think, although my servers use APT so I cant test it. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 try yum upgrade and see if php gets updated, there will probably be tones of other stuff to update too so let it do everything. If you still have the old version of php then you know you have to compile it. Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 I did the "yum update", and it updated all kinds of stuff. After the update I rebooted the machine and did a phpinfo() and everything is the same, ie php 5.1.6. does that mean I need to compile it? try yum upgrade and see if php gets updated, there will probably be tones of other stuff to update too so let it do everything. If you still have the old version of php then you know you have to compile it. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 it suggests you do yeah. You might be able to install the package version over the current compiled one, but you might run into problems. I'm a Ubuntu user so don't really know how your OS works. Maybe better to find a pro and get them to do it. Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks for the suggestions. I appreciate your help thus far. it suggests you do yeah. You might be able to install the package version over the current compiled one, but you might run into problems. I'm a Ubuntu user so don't really know how your OS works. Maybe better to find a pro and get them to do it. Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 I actually got PHP to update using the directions found at http://en.ispdoc.com/index.php/Updating_PHP_in_CentOS_Linux#Updating_PHP_and_MySQL However, the cookie problem is still there even with php 5.3.6. I'm thinking it must be a setting in php.ini, maybe a setting somewhere???? it suggests you do yeah. You might be able to install the package version over the current compiled one, but you might run into problems. I'm a Ubuntu user so don't really know how your OS works. Maybe better to find a pro and get them to do it. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 ah, good job There don't seem to be any ini setting related to cookies at all :? Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 Could it be a cache setting in the php.ini file? Hmmmmm... ah, good job There don't seem to be any ini setting related to cookies at all :? Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 Well, any settings that exist will just control the header. And form what you posted the header looks right. Lets go back to the original code as you say the problem must be there ... Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 can you post the actual full script you were using. Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 Here you go... <?php ini_set('date.timezone', 'America/Los_Angeles'); $expire = time() + 60*60*24*30; // 30 days $value = "my cookie value"; setcookie("TestCookie",$value, $expire, '/', '.mydomain.com'); ?> Do you think the mod_expire module (which is installed per the phpinfo) could have anything to do with it. See apache doc here http://httpd.apache.org/docs/2.0/mod/mod_expires.html can you post the actual full script you were using. Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 the mod_expire module does nto seem to have anything to do with cookie, but you could try disabling it and see if it helps. Your code looks fine, I'm out of ideas Quote Link to comment Share on other sites More sharing options...
siteturbo Posted March 30, 2011 Author Share Posted March 30, 2011 When I do a var_dump(headers_list()); This is what I get: array(5) { [0]=> string(23) "X-Powered-By: PHP/5.3.6" [1]=> string(38) "Expires: Thu, 19 Nov 1981 08:52:00 GMT" [2]=> string(77) "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" [3]=> string(16) "Pragma: no-cache" [4]=> string(77) "Set-Cookie: TestCookie=my+cookie+value; expires=Fri, 29-Apr-2011 21:57:57 GMT" } Why does it say Expires: 1981? Quote Link to comment Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 because you seem to have caching disabled and this is one of the ways of preventing a browser caching the page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.