Okay. Lately I tried to make a code to check to see if a variable has a cookie inside it then display, Welcome "cookie name". But, it won't execute properly! Here's the code <?php $me=$_COOKIE['user']; if(strlen($_COOKIE['user'])==0){ setcookie('user','david',60,'/'); print "Cookie Not There!"; }else{ print "Welcome ".$me; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> </body> </html> PHP: You see the code in action here
Use the empty function: www.php.net/empty Edit: Appears someone just beat me to it. Here's an example to make up for it: if(empty($_COOKIE['user'])){ setcookie('user','david',60,'/'); print "Cookie Not There!"; }else{ print "Welcome ".$me; } PHP: Jay
to be honest, you can even do if (!$_COOKIE['name']) {... } which will be fine - will evaluate as false if it's not set. only danger is if it IS set and the value is 0 or false. isset() is the safest way to go.