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, '/');
Now that we can save a cookie the next thing we need to do is read the cookie and 0:00 show the name on the welcome page. 0:04 So we open index.php. 0:07 The cookie variable works just like the session variable, 0:09 so let's loop through the array. 0:12 If is set cookie, for 0:17 each cookie as key and value. 0:21 Echo key. 0:34 And the we'll add a line break. 0:37 Let's take a look at this in the browser. 0:39 We'll step through our story. 0:42 Then from our story page, we choose save story. 0:58 We're redirected to the welcome page where our cookies are displayed. 1:02 You'll notice that we have a cookie named PHPSESSID. 1:06 This is how php ties sessions to a browser. 1:10 Let's go back to work spaces and add the ability to set a cookie and 1:14 rewrite the story. 1:18 First we don't want to read the PHPSESSID. 1:20 If key is not equal to PHPSESSID Then, 1:29 we want to add a div to style these links. 1:38 Next we add a link. 1:55 We're going to send them to our cookie file again. 2:06 This time with the read 2:08 parameter set to the file. 2:12 Urlencode the key. 2:17 We added the timestamp to the end of our cookie, so lets display the name and 2:29 then a formatted date instead of the timestamp. 2:33 And the name all together. 2:36 To get just the name we can use substring. 2:39 We start with the 0, and we end with -10 because, time stamps are ten characters. 2:47 Add a space between the name and the date. 2:56 And now, we can use the date function to format the date. 3:00 Day, month, year. 3:08 Hour, minute, second. 3:11 Now we use substring again, this time starting with -10. 3:15 Our cookie name is a string. 3:21 Even though the last 10 characters that we're pulling are numbers. 3:23 The date function requires a timestamp, so 3:27 we need to cast the timestamp string to an int. 3:30 Now we can update our cookie script to read the cookie's value. 3:39 Back in cookie.php, we're going to 3:43 add elseif (isset($_GET['read]. 3:48 To read the cookie, we want to split the cookie string into the individual values, 4:00 and update the session variables. 4:05 We'll use explode. 4:10 We tell it to use the colon as our delimiter. 4:13 And then Cookie Get Read. 4:18 This will give us a word array. 4:28 But it will start with the zero index. 4:30 We can use array_combine to specify the keys we wish to use. 4:33 The first parameter in array_combine are the keys, 4:37 the second parameter are the values. 4:41 So for our keys, we want to arrange, of 1 to five. 4:44 Now we can redirect to our story page. 4:52 Let's give this a try in the browser. 5:04 Refresh the Welcome page. 5:09 And now we can click on our cookie. 5:11 This reads the contents of the cookie and sets the session variables. 5:15 So that when we're redirected to the story page our stories written properly. 5:19 One last thing we should add is the ability to delete a cookie. 5:23 Let's go back to workspaces. 5:27 In our cookie file we can add another else if to check for delete, 5:29 To delete a cookie we set the cookie to an empty string that expires in the past. 5:45 Making sure to use the same path we used for original cookie. 5:51 Set cookie. 5:56 Empty string, time minus 3,600, which is one hour. 6:04 And then, the root path. 6:14 After this action, we also want to redirect to our index.php. 6:17 So let's move this outside the if statement. 6:22 Now let's go back to index.php and add a link to delete the cookie. 6:29 We'll copy this first link we have. 6:37 We'll change btn-info to btn-danger. 6:42 We'll also change read to delete. 6:47 Then instead of the cookie name we'll just use X. 6:54 Let's try out this last feature in the browser. 6:59 Choose other stories and click on the X next to the cookie name. 7:05 Boom our cookie is gone. 7:11
You need to sign up for Treehouse in order to download course files.Sign up