Cookie Problem - Can't Set Future Expiration

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() + 606024*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.

Anybody have any suggestions? Still can’t figure out why my server won’t set a persistent cookie.

At first I thought I was seeing double when I saw this post on another forum… then seeing your follow up I realised I was seeing double-double vision lol