This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
Documentation
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[1]', 'value1');
setcookie('cookiename[2]', 'value2');
// 2. retreiving array values
echo $_COOKIE['cookiename'][0];
echo $_COOKIE['cookiename'][1];
// 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, '/');
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up