Page 1 of 1
Problem with setcookie
Posted: Wed Feb 13, 2008 1:49 pm
by visonardo
hi, i have this problem:
Code: Select all
if(setcookie("djperga","bien",420,"/",'.dominio.com'))
header("Location: admin.php");
supossedly the cookie is generated because im redirected to admin.php, but then in admin.php $_COOKIE['djperga'] dont exist, $_COOKIE is empty. What is wrong here? im use other sites that need cookie and works perfectly but now that im trying generate a cookie from my site dont work. What is wrong here?
Greetings, visonardo.

Re: Problem with setcookie
Posted: Wed Feb 13, 2008 3:58 pm
by Christopher
Check the session and cookie manual pages. Because redirect stay within the same request you may need to do session_write_close() or something to actually send the headers/cookie.
Re: Problem with setcookie
Posted: Wed Feb 13, 2008 5:50 pm
by visonardo
nothing, i put session_write_close() after setcookie and dont work.

Re: Problem with setcookie
Posted: Wed Feb 13, 2008 6:37 pm
by Christopher
Have you read the Notes in the manual entry for setcookie()? You may need to use meta-refresh.
Re: Problem with setcookie
Posted: Wed Feb 13, 2008 7:52 pm
by visonardo
Code: Select all
<?
if($_COOKIE['hello']){
echo 'Yeah!';
}else{
setcookie('hello','yes',10000000,'/','domain.com');
echo '<a href="tst.php">dale!</a>';
}
?>
always show "dale!" and i do click there and again

(i did it to test)
Re: Problem with setcookie
Posted: Wed Feb 13, 2008 8:07 pm
by Christopher
The expire parameter is a Unix timestamp. The value 10000000 is in the past. If you want the future it would be time()+86400 where 86400 is one day.
Re: Problem with setcookie
Posted: Wed Feb 13, 2008 8:11 pm
by visonardo
ohhhhhhh, yes! jajaja, i forgot time()+10000000 thank!