Check if cookie exists in php

In PHP, the “isset()” function can be used to check if a cookie exists. This function returns true if a specified variable is set, not null and false otherwise.

Here is some sample code that shows how to use the isset() function to check if a cookie called “YourCookieName” exists:

<?php

if(isset($_COOKIE["YourCookieName"])){
    echo "Cookie exists";
} else{
    echo "Cookie doesn't exist";
}

?>

You can also use the array_key_exists() function to check for the existence of a specific key in an array.

<?php

if (array_key_exists('YourCookieName', $_COOKIE)) {
    echo "Cookie exists";
} else{
    echo "Cookie doesn't exist";
}
?>

Cookies are case-sensitive, therefore when checking for the existence of a cookie that you set with the name ‘YourCookieName’, you must check for the same exact case ‘YourCookieName’ and not ‘yourCookieName’ or ‘yourcookiename’.

Also, if a client has cookies disabled, the $_COOKIE array will be empty, for this reason, you should always be prepared to handle an empty $_COOKIE array.

You can also check the value of the cookie to see if it’s null or not. If the cookie is not set, it will return NULL. Here is an example code for the same:

<?php

if ($_COOKIE['YourCookieName'] !== NULL) {
    echo "Cookie exists";
} else{
    echo "Cookie doesn't exist";
}
?>