Alex Rivera | Logout

How would I test if a cookie is set using php and if it's not set do nothing

Asked 2011-05-02T15:57:05.093
21

I've tried

 $cookie = $_COOKIE['cookie'];

if the cookie is not set it will give me an error

PHP ERROR
Undefined index: cookie

How would I prevent it from giving me an empty variable>

Edit
Report

1 Answer

19

You can use array_key_exists for this purpose as follows:

$cookie = array_key_exists('cookie', $_COOKIE) ? $_COOKIE['cookie'] : null;
answered 2011-05-02T15:59:20.257

Your Answer