views:

209

answers:

5

Hi All, 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

A: 

One browser could reacting more quickly to the header redirect you're doing then the other.

Try turning the commands around:

 setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/');
 header("location: test2.php");
Pekka
I have done this and have also put sleep(2) between these two lines but still no difference :-(
matt
The order of these statements should have no affect on the response. According to RFC 2616 (HTTP 1.1), section 6.1 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html), the `Status` header is the first line of the response, followed by other headers (of which the cookie is one).
Steve Madsen
A: 

looking at your example, you have got header() first then setcookie(). Try and setcookie() first and then do the header();

PHPology
+1  A: 

I also have this problem. I noticed this on the php website from somebody.

When setting a cookie on a page that redirects, the cookie must be set after the call to header('Location: ....');

http://php.net/manual/en/function.setcookie.php

I'm still unsure

snailpaceProgrammer
+1  A: 

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.

Brad F Jacobs
A: 

Some browsers prevent the setting of cookies before user interaction has occurred. I know Safari does, and I believe IE works the same way. Basically, all cookies will be ignored on the first response received from your site. I suspect that if you instead try something like the following, it will work as expected:

test0.html

<html>
  <body>
    <a href="test1.php">force user interaction</a>
  </body>
</html>

test1.php

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

test2.php

<?php
  var_dump($_COOKIE);
?>
Brandon Gano