2uzhan.com
Advertisement
Now Place:2uzhan.com » Having trouble reading cookie values

Having trouble reading cookie values

PHP @ July 5, 2003   Views:0

I've got three scripts that I wrote to see if I was setting cookies properly. Here they are.

PHP Code:

<?
//setcookie.php sets a cookie with two values (user_id and user_name)

setcookie('mycookie[user_id]', '1', '60', '/');
setcookie('mycookie[user_name]', 'joe smith', '60', '/');

echo "<a href='showcookie.php'>show cookie values</a>";
?>

PHP Code:

<?
//showcookie.php checks to see if the cookies are set, and shows the values

if (isset($HTTP_COOKIE_VARS['mycookie[user_id]']))
    {
         echo $HTTP_COOKIE_VARS['mycookie[user_id]'];
         echo $HTTP_COOKIE_VARS['mycookie[user_name]'];
    }
else
    {
         echo "no cookie set";
    }

echo "<br><a href='setcookie.php'>set a cookie</a><br>";
echo "<a href='removecookie.php'>remove a cookie</a>";
?>

PHP Code:

<?
//removecookie.php removes the cookies

setcookie('mycookie[user_id]', '', '60', '/');
setcookie('mycookie[user_name]', '', '60', '/');

echo "<a href='showcookie.php'>click</a>";
?>

I'm accessing setcookie.php first then going to showcookie.php. I keep getting the "no cookie set" message even though the cookie is there. I've checked my Temporary Internet Files to verify it. Any ideas?

--------------Solutions-------------

try setting the cookie like this

PHP Code:

setcookie ("mycookie[0]", "1", time()+7200);
setcookie ("mycookie[1]", "joe smith", time()+7200); 

then to call the cookies to echo them try this

PHP Code:

echo $_COOKIE["mycookie"][0];
echo $_COOKIE["mycookie"][1]; 

hope this helps

Will do. Thanks.

Hang on. That's not really what I was asking. I'm able to call the values. My real question is why is the isset function not working. If I comment that all out, I can see the values. When I put it back in it tells me that there aren't any cookies because

PHP Code:

isset($HTTP_COOKIE_VARS['mycookie[user_id]']) 

isn't returning anything.

You might try using array_key_exists() instead of isset(), sometimes you can get around NULL values that way.

That said, the problem may well be in the array syntax, although it looks OK.

Do a print_r($_COOKIE) and see if the cookies are there but are named something other than you expect. And definitely do:

PHP Code:

$testvar=isset($_COOKIE['mycookie[user_id]']);
echo $testvar; 

Somewhere around here was a thread on testing with isset() being rather unique...but I can't remember the whole thing...

HTH,

Aw, heck, it could just be your using the literal strings, too.

Try double quotes instead....

It's getting late, I need sleep

Tags:
© 2018 2uzhan.com Contact