Php SetCookie works in Firefox but not IE

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); ?> 

Then I point my browser to test.php, which redirects to test2.php. Then I 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 intentionally X'd from PHPSESSID above!

Does anyone know where I am going wrong and why IE6 is not showing my cookie.

Thanks in advance

+4
source share
5 answers

Do you work in a localhost environment? IE http: // localhost check? If so, this may cause some problems with the cookie set. My suggestion sets a domain field for setcookie, if you are working with localhost try this: setcookie("username", "George", false, "/", false); or install vhost with a server name other than localhost and use this for the domain.

Setting a cookie with a domain will look something like this:

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

Hope this helps you.

+3
source

I also have this problem. I noticed this on a php website from someone.

When setting the cookie on the redirected page, the cookie must be set after the header is called ('Location: ....');

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

I'm still not sure

+1
source

One browser may respond more quickly to the header redirection you are doing, and then another.

Try turning on the commands:

  setcookie("test", "8kFL4IZfjkBmV7AC", time()+60*60, '/'); header("location: test2.php"); 
0
source

looking at your example, you have a header () and then setcookie (). First try and setcookie (), then make the header ();

0
source

Some browsers prevent the setting of cookies before interacting with the user. I know what Safari does, and I believe that IE works the same. Basically, all cookies will be ignored on the first response received from your site. I suspect that if you 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); ?> 
0
source

Source: https://habr.com/ru/post/1316474/


All Articles