Unexpected behaviour of cookie

Messages
89
Reaction score
0
Points
6
My login verify script -

Code:
if($pass == $hash)
setcookie("cookiename",$cookieVal);

This stores the cookie in the broswer. No problems with this; I have checked it myself.

However in a different page when I check to see if the cookie exists, it returns false.

Code:
if(isSet($_COOKIE['cookiename'])) echo "Cookie is set";
else echo "Not set";

The above code prints "Not set" in the broswer window.

Also, if I try
Code:
echo $_COOKIE['cookiename'];
the ouput is an error Notice: Undefined index: cookiename

Apparently the browser (Firefox) cannot identify the cookie, although I can see it present when I check my broswer's options tab! Later, I also tried in Chrome and IE - same issue.

What am I doing wrong?


Update: When I added
Code:
if(isSet($_COOKIE['cookiename'])) echo "Set";
to the end of the login verify script, my cookie was detected ("Set" was printed)! But it isn't being detected in any other page.
 
Last edited:
Messages
89
Reaction score
0
Points
6
Okay, problem solved. I was missing the path parameter. The code should be one of the following -

Code:
setcookie("cookiename",$cookieVal,0,'/');
Code:
setcookie("cookiename",$cookieVal,time()+60*60*24*365,'/');
 
Last edited:
Top