tags:

views:

34

answers:

3

Hi all, What does _COOKIE variable contain. Does it just contain the cookie that is sent by the browser for the current request?

+4  A: 

That is correct!

$_COOKIE

The value of $_COOKIE is determined by the content of cookies received in the user agent's request.

Frankie
+1  A: 

Pretty much on the dot...

You basically use $_COOKIE to get the data stored in cookies from the browser

Of course cookies are not always very reliable, as they can be turned off by the client but nonetheless used widely.

Mark
+1  A: 

Contents of the $_COOKIE superglobal variable:

When retrieving cookies with PHP using the superglobal $_COOKIE an associative array of variables passed to the current script via HTTP Cookies is returned.

To inspect all the cookie variables simply use:

print_r($_COOKIE);

To retrieve the value of a specific cookie variable, reference the key of the cookie variable:

echo $_COOKIE["myVariableName"];

The trickiest thing about retrieving cookies with PHP is that a cookie variable will not be available until the request after it is set. So you cannot access a cookie with PHP until after the next page load:

// Cannot have output before setting cookies. 
// Cookie will be sent along w the rest of the HTTP headers.
setcookie("name", "Pat");

// If the above was the first time "name" was set,
// this will echo NOTHING!!!
echo $_COOKIE["name"];

// You will only be able to retrieve $_COOKIE["name"] after the
// next page load.
Peter Ajtai