Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>

like image 346
Aaron Avatar asked May 02 '11 15:05

Aaron


People also ask

How check cookie is set or not in PHP?

PHP Cookies Checking if a Cookie is Set Use the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.

How do you check if cookies are set or not in laravel?

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')) { ... }

How can I see cookies in PHP?

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.


1 Answers

Use isset to see if the cookie exists.

if(isset($_COOKIE['cookie'])){
    $cookie = $_COOKIE['cookie'];
}
else{
    // Cookie is not set
}
like image 152
Rocket Hazmat Avatar answered Oct 26 '22 04:10

Rocket Hazmat