Reading and Deleting Cookies7:14 with Alena Holligan
Reading cookie variable works like reading session and other variables. We can access one directly or look over the array of cookie variable. To delete a cookie, we set the cookie to an empty string that expires in the past. Making sure to use the same path we used for the original cookie.
setcookie() — Send a cookie
implode() — Join array elements with a string
explode() — Split a string by string
urlencode() — URL-encodes string
range() — Create an array containing a range of elements
array_combine() — Creates an array by using one array for keys and another for its values
date() — Format a local time/date
For date formatting, see the date() function.
Type Juggling — Casting to an integer and other type casting
Retrieving a Cookie
// assuming the name is 'cookiename', retrieve a single value echo $_COOKIE['cookiename']; // assuming the name is 'cookiename' // 1. setting array values by specifying key setcookie('cookiename', 'value1'); setcookie('cookiename', 'value2'); // 2. retreiving array values echo $_COOKIE['cookiename']; echo $_COOKIE['cookiename']; // will display value1value2
Deleting a Cookie
To delete a cookie, we set the cookie to an empty string that expires in the past. Making sure to use the same path we used for the original cookie.
// Where $_GET['delete'] is the name of the cookie setcookie($_GET['delete'], "", time() - 3600, '/');
You need to sign up for Treehouse in order to download course files.Sign up