Checking if a Cookie is Set
suggest changeUse the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.
Example:
if (isset($_COOKIE['user'])) {
// cookie is set (including an empty value)
echo 'User is ' . $_COOKIE['user'];
} else {
echo 'User is not logged in';
}
// PHP 7.0+. The fallback must not repeat "User is".
echo 'User is ' . ($_COOKIE['user'] ?? 'not logged in');
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents