$_COOKIE
This YouTube video was created by Steve Griffith.
The $_COOKIE
variable is an associative array of variables passed to the current script via HTTP Cookies, which can be defined using the setcookie()
function.
Cookies are ways for websites to store bits of data in the browser could then be used for numerous application like identifying and tracking users. Beyond the name and value of a cookie, other options can be set when creating a cookie, including which subdomain to use and whether is the cookie should be passed over HTTPS.
Set a Cookie
The setcookie()
function is used to set a new cookie. With the setcookie()
, all possible cookie options can be set, but only the name is required.
// setcookie(name, value, expire, path, domain, secure, httponly)
setcookie('food', 'pizza', time() + 1440, '/', 'localhost', TRUE, FALSE)
The code above will create a food
cookie with the value of 'pizza'. It will expire in 24 minutes and is only available on the secure version of localhost.
Access a Cookie
PHP makes all set and accessible cookies available through the $_COOKIE
variable. Assuming a cookie was set using the code above, that cookie could be accessed with the following:
echo $_COOKIE['food'] // pizza