php SetCookie works in Firefox, but not IE

matt picture matt · Jul 21, 2010 · Viewed 10.4k times · Source

I have two php scripts

test.php

<?php
 header("location: test2.php");
 setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
 exit;
?>

test2.php

<?php
 var_dump($_COOKIE);
?>

I then point my browser to test.php which redirects to test2.php. I then get the following results.

In firefox, i get the following:

array
  'test' => string '8kFL4IZfjkBmV7AC' (length=16)

However in IE6, i get the following:

array
  'PHPSESSID' => string 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' (length=32)

note: i have purposely X'd out the PHPSESSID above!

Does anybody know where i am going wrong and why IE6 isnt showing my cookie.

Thanks in advance

Answer

Jimithus picture Jimithus · Jul 22, 2010

Are you working on a localhost environment? IE http://localhost to test? If so this can cause some issues with the set cookie. My suggestion is setting the domain field for the setcookie, if you are working on localhost try this: setcookie("username", "George", false, "/", false); or set a vhost with a servername other than localhost and use that for the domain.

Setting the cookie with the domain would be something like:

setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/', '.domain.com');

Hopefully that helps ya out.