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>
PHP Cookies Checking if a Cookie is Set Use the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.
Forum Check if cookie exists and is not null Last updated 3 months ago. You can get cookies from request: $value = Request::cookie('name', $defaultValue); if (Request::hasCookie('phone')) { ... }
Accessing Cookies with PHP Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables. Following example will access all the cookies set in above example. You can use isset() function to check if a cookie is set or not.
Use isset
to see if the cookie exists.
if(isset($_COOKIE['cookie'])){
$cookie = $_COOKIE['cookie'];
}
else{
// Cookie is not set
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With