By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

Testing the presence of a given cookie

P: n/a

Hi,

How do I test if a user has a given cookie? O:-)

Thanks
Jan 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Fernando Rodríguez wrote:
Hi,

How do I test if a user has a given cookie? O:-)

Thanks


Here is how I do it:

//set the cookie:
setcookie("CookieTest", "t");

//test of the cookie exists (can only be done in the next page / load)
if($_COOKIE["CookieTest"] == "t") {
echo "cookie exists";
}
HTH

- Nicolaas
Jan 16 '06 #2

P: n/a
Hello windandwaves,
How do I test if a user has a given cookie? O:-)

Thanks

Here is how I do it:

//set the cookie:
setcookie("CookieTest", "t");
//test of the cookie exists (can only be done in the next page / load)
if($_COOKIE["CookieTest"] == "t") {
echo "cookie exists";
}

Thanks it works. However, if the cookie doesn't exist, I get a warning on
the rendered page similar to this one:
Notice: Undefined index: aTestCootie in c:\proyectos\websites\test\action.php
on line 13

That's great for debugging, but how do Iavoid this from showing up in production
code?

Thanks
Jan 17 '06 #3

P: n/a
Fernando Rodríguez wrote:
Hello windandwaves,
How do I test if a user has a given cookie? O:-)

Thanks

Here is how I do it:

//set the cookie:
setcookie("CookieTest", "t");
//test of the cookie exists (can only be done in the next page / load)
if($_COOKIE["CookieTest"] == "t") {
echo "cookie exists";
}


Thanks it works. However, if the cookie doesn't exist, I get a warning
on the rendered page similar to this one:
Notice: Undefined index: aTestCootie in
c:\proyectos\websites\test\action.php on line 13

That's great for debugging, but how do Iavoid this from showing up in
production code?

Thanks


if (isset($_COOKIE['CookieTest'] && $_COOKIE['CookieTest'] == 't') ...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.