[Resolved] header faster than setcookie?
I've been wondering about this for some time now and now I need to have an answer for work.
When I write something like the following, the cookie doesn't get set:
Code:
<?
@setcookie("lister_id", $_GET['l'], time()+18144000, "/");
@header('Location: '.base64_decode($_GET['re']));
?>
But, when I replace PHP's header() with a client-side redirect the cookie gets set properly:
Code:
<?
@setcookie("lister_id", $_GET['l'], time()+18144000, "/");
echo '<script> document.location="'.base64_decode($_GET['re']).'"; </script>';
?>
The only thing I can think of is that since setcookie() has to deal with an actual file (the cookie on the client's machine) that the first command is not getting executed by the time the server gets to the header() command. Is that what's going on? Also, is there some way I can slow down this process so I don't have to incorporate a JavaScript redirect?
Many thanks.
Re: header faster than setcookie?
PHP scripts are not multithreaded. It is impossible for the statements to be executed out of sync.
Remove the error-suppression operator (@) from both calls and then see what happens. Suppressing errors should never be done unless you have a very good reason.
I am not sure, but it might well be the case that browsers will not accept cookies set during a redirection.
Re: header faster than setcookie?
Thanks penagate. I've written the code above many times without the error suppressor and still had the same problem. Perhaps your idea of browsers not accepting cookies on a redirect might be the case here.