Help with weird setcookie() behavior

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
jchonphoenix
Forum Newbie
Posts: 2
Joined: Sun Aug 24, 2008 9:40 pm

Help with weird setcookie() behavior

Post by jchonphoenix »

Hello guys, I'm trying to set a user authentication cookie and im getting some odd errors.

basically this is my command for setting the cookies: setcookie("auth", "1", 0, "/", "domain.com", 0);

and when i store that in a variable it returns true. Hoewver, in $_COOKIE['auth'] the cookie is never set. and when i go to view cookies in my browser it doesn't show up. Help?
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Help with weird setcookie() behavior

Post by Ziq »

It's very strange.

Try this code:

Code: Select all

<?php
setcookie("auth", "1");
print_r($_COOKIE);
?>
Necessarily reload page in browser.
jchonphoenix
Forum Newbie
Posts: 2
Joined: Sun Aug 24, 2008 9:40 pm

Re: Help with weird setcookie() behavior

Post by jchonphoenix »

setcookie("auth", "1");
worked. Thanks. However, Can anyone give an explanation for why this is? thanks
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Help with weird setcookie() behavior

Post by Ziq »

jchonphoenix wrote:setcookie("auth", "1");
worked. Thanks. However, Can anyone give an explanation for why this is? thanks
Read set_cookie()

I think you not correct use parameters path and domain.
Post Reply