Jump to content

Cookie Problem: Can't set Future Expiration


siteturbo

Recommended Posts

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.