Problem with setcookie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Problem with setcookie

Post 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. :?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with setcookie

Post 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.
(#10850)
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Re: Problem with setcookie

Post by visonardo »

nothing, i put session_write_close() after setcookie and dont work. :?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with setcookie

Post by Christopher »

Have you read the Notes in the manual entry for setcookie()? You may need to use meta-refresh.
(#10850)
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Re: Problem with setcookie

Post 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)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with setcookie

Post 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.
(#10850)
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Re: Problem with setcookie

Post by visonardo »

ohhhhhhh, yes! jajaja, i forgot time()+10000000 thank!
Post Reply