PDA

View Full Version : Reading cookies


Richard
11-13-2002, 03:21 PM
I managed to set a cookie but the example code on php.net for reading a cookie doesn't work O.o


<?
echo $Visual;
echo $_COOKIE['Visual'];
?>


:rolleyes:

firepages
11-13-2002, 04:12 PM
how do you know it is set if you cant read it ?


<?
if(!$_COOKIE["cookie_name"]){
setcookie("cookie_name","cookie_value",time()+3600);
header("location:$_SERVER[PHP_SELF]");
}else{
echo $_COOKIE["cookie_name"];
}
?>


note that in IE6 you will have to set the custom security level to non-existant.

Richard
11-13-2002, 05:28 PM
Because i looked in tempory internet files.

I'll try that code..

firepages
11-13-2002, 05:37 PM
Originally posted by Richard
Because i looked in tempory internet files.



:D fair enough !

Richard
11-13-2002, 06:04 PM
Grr

I used that exact code you gave and the php page comes up with "You are not authorized to view this page" (even though it sets the cookie, and it still does it when i refresh)

I checked what IE sent:

Cookie: cookie_name=cookie_value

..

mordred
11-13-2002, 08:58 PM
What code did you use? firepages example did not include any message to the user.

Also, check which cookie values are read by PHP by using


var_dump($_COOKIE);


If that always prints nothing or null, maybe you run on an old PHP version...? $_COOKIE is available since PHP 4.1, prior to that you have to use $HTTP_COOKIE_VARS.

Richard
11-13-2002, 09:49 PM
*sigh*

That figures. We're on 4.0.6 :/

How does $HTTP_COOKIE_VARS work... :S

Richard
11-13-2002, 10:15 PM
echo ("$HTTP_COOKIE_VARS");

just displays "Array" on the page

echo var_dump($HTTP_COOKIE_VARS);

does "array(0) { }"

And i did re-set the cookie.

???

Nightfire
11-13-2002, 10:59 PM
$HTTP_COOKIE_VARS['cookiename']

Richard
11-13-2002, 11:07 PM
Originally posted by Nightfire

$HTTP_COOKIE_VARS['cookiename']


I used that and:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/domains/abcd.com/user/htdocs/test/test.php on line 2

Lines two is:

echo ("$HTTP_COOKIE_VARS['TestCookie']");

Richard
11-18-2002, 01:38 PM
<?

if ($HTTP_COOKIE_VARS[yourcookiename]) {
echo "Cookie: $HTTP_COOKIE_VARS[yourcookiename]";
}

else {
echo "No cookie";
}

?>


This finally works. But how do you check if the cookie value is equal is 'true' or something ?