Forum Moderators: coopster

Message Too Old, No Replies

Setting and checking if cookie exists.

I have the code (I think) but why isn't it working?

         

scraptoft

4:31 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



I know I know, this question is asked all over the internet but despite reading so much about it I just can't get it to work.

SET cookie:
$value = 'value';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */

CHECK cookie:
if(isset($HTTP_cookie_VARS['TestCookie'])){;
$cookievalue = $HTTP_cookie_VARS['TestCookie'];
}else{
$cookievalue = no_cookie;
}
echo "$cookievalue";

When I echo $cookievalue I get no_cookie. I can't find out what I am doing wrong. I have checked in firefox and the cookie exists.

cameraman

4:35 pm on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Variables in php are case sensitive - the cookie array is:
$HTTP_COOKIE_VARS

or you can use
$_COOKIE

scraptoft

4:38 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Cameraman, thank you so much for pointing out my cookie idiocy.

(Can't believe I have wasted so much time trying to get that to work.)

What's the difference between $HTTP_COOKIE_VARS and $_COOKIE?

cameraman

5:09 pm on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_COOKIE's shorter!

There's no functional difference.
The first form has been around since php3, and the second was invented in php 4.1.

scraptoft

5:16 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Thanks again, I hope i didn't burn your brain out too much with those two difficult questions ;-)

Cheers,

cameraman

5:36 pm on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're welcome.
LOL I think I'll be ok!

jatar_k

11:57 am on Oct 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



HTTP_COOKIE_VARS is deprecated and slightly different

[php.net...]
[php.net...]

joelgreen

1:00 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



Please also note that following will not work:

setcookie("TestCookie", $value);

// CHECK cookie
if(isset($_COOKIE['TestCookie'])){;
// do something
}

Cookie is browser side thingy, so it will not be available in the $_COOKIE array until sent by the browser. So in example above cookie will become available only after page is accessed second time.

scraptoft

1:32 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



Thanks jatar will read up on them.

Cookie is browser side thingy, so it will not be available in the $_COOKIE array until sent by the browser. So in example above cookie will become available only after page is accessed second time.

Your saying if both the codes are in the same file, the cookie won't be 'seen' until the browser has been refreshed etc?

I am using setcookie.php and using a location header to checkcookie.php so (if I understand you right) this isn't an issue for me. - just clariying.

Cheers guys.

joelgreen

5:27 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



scraptoft, yes that is what i was trying to say :)