1 00:00:00,640 --> 00:00:02,280 We have our story game working. 2 00:00:02,280 --> 00:00:05,420 We can step through the game as many times as we'd like, and 3 00:00:05,420 --> 00:00:08,110 the story changes with each play. 4 00:00:08,110 --> 00:00:12,320 Sessions provided an easy way to keep the required variables available 5 00:00:12,320 --> 00:00:14,200 throughout the site. 6 00:00:14,200 --> 00:00:17,750 One of the main features and limitations of sessions 7 00:00:17,750 --> 00:00:21,820 is that they are only available for the current user session. 8 00:00:21,820 --> 00:00:24,760 They terminate when the browser is closed, or 9 00:00:24,760 --> 00:00:27,200 after a set amount of time with no activity. 10 00:00:28,280 --> 00:00:32,030 There are often times when we want to keep some information available for 11 00:00:32,030 --> 00:00:34,070 a longer period of time. 12 00:00:34,070 --> 00:00:39,410 Storing a zip code preference for location based searches or to calculate shipping. 13 00:00:39,410 --> 00:00:41,420 Storing a referrer reference, so 14 00:00:41,420 --> 00:00:45,510 affiliates get credit when someone purchases a product or service. 15 00:00:45,510 --> 00:00:48,670 Storing shopping cart data for people who are not yet logged in. 16 00:00:49,990 --> 00:00:52,782 All of these require that we keep data persistent for 17 00:00:52,782 --> 00:00:55,180 longer than a user's session. 18 00:00:55,180 --> 00:00:57,810 We can do this by using cookies. 19 00:00:57,810 --> 00:01:01,120 A cookie is a piece of data sent from a website and 20 00:01:01,120 --> 00:01:05,255 stored on the users computer by the user's web browser. 21 00:01:05,255 --> 00:01:08,320 Let's take a look at how PHP accesses cookies 22 00:01:08,320 --> 00:01:11,430 by adding the ability to save a story. 23 00:01:11,430 --> 00:01:16,437 Create a new file in the inc directory and we'll name it cookie.php. 24 00:01:20,993 --> 00:01:23,096 We want to grab our session variables, 25 00:01:23,096 --> 00:01:25,910 so add session_start(); at the top of the file. 26 00:01:28,550 --> 00:01:32,540 We'll be using this file to write, read, and delete a cookie. 27 00:01:32,540 --> 00:01:34,710 So we need to know which action we're performing. 28 00:01:36,120 --> 00:01:41,735 if (isset($_GET['save])). 29 00:01:46,525 --> 00:01:51,280 If we're saving a cookie, first we need to come up with a unique name for the cookie. 30 00:01:51,280 --> 00:01:56,290 If the name is not unique, it will write over the cookie that already exists. 31 00:01:56,290 --> 00:01:58,420 We could just use a timestamp, but 32 00:01:58,420 --> 00:02:02,980 we want people to be able to distinguish the story they want to read. 33 00:02:02,980 --> 00:02:07,450 I probably won't remember what story I wrote at which time, but 34 00:02:07,450 --> 00:02:11,860 I'll be more likely to remember if I'm shown the name that I used with the story. 35 00:02:11,860 --> 00:02:15,450 So let's use the name from the story, along with the timestamp. 36 00:02:15,450 --> 00:02:18,040 The name should be a simple string of letters. 37 00:02:18,040 --> 00:02:20,519 But we'll use urlencode just to be sure. 38 00:02:27,783 --> 00:02:30,310 To set a cookie we use setcookie. 39 00:02:32,390 --> 00:02:37,430 The first parameter is the name, and the second parameter is value. 40 00:02:37,430 --> 00:02:39,450 Value has to be stored a string. 41 00:02:39,450 --> 00:02:44,510 This could be a serialized version of an array, a JSON object, 42 00:02:44,510 --> 00:02:48,705 or any other string representation of an object or an array. 43 00:02:48,705 --> 00:02:51,265 We can also turn a cookie into an array by 44 00:02:51,265 --> 00:02:53,615 adding square brackets to the end of our name. 45 00:02:53,615 --> 00:02:58,025 But's let use implode to create a string from our session variables. 46 00:02:58,025 --> 00:02:59,665 We'll separate them with a colon. 47 00:03:06,403 --> 00:03:09,280 That's technically all you need to set a cookie. 48 00:03:09,280 --> 00:03:11,240 But it won't work for what we want. 49 00:03:11,240 --> 00:03:14,420 The third parameter is an expiration time. 50 00:03:14,420 --> 00:03:18,710 If this is not set, the cookie will expire when the session is closed. 51 00:03:18,710 --> 00:03:21,340 So we aren't gaining anything there. 52 00:03:21,340 --> 00:03:23,410 Let's set this to 30 days. 53 00:03:23,410 --> 00:03:27,650 String to time will turn this string into a time stamp. 54 00:03:27,650 --> 00:03:30,680 The fourth parameter is the path on the server 55 00:03:30,680 --> 00:03:32,610 in which the cookie will be available. 56 00:03:32,610 --> 00:03:36,130 By default the current path is used, which would mean 57 00:03:36,130 --> 00:03:40,450 that the cookie would only be accessible from within the inc folder. 58 00:03:40,450 --> 00:03:44,690 We could make that work but let's set the path to the root of our site. 59 00:03:44,690 --> 00:03:47,650 These are the parameters we need for our project, but 60 00:03:47,650 --> 00:03:51,740 let's check out the documentation for the last three parameters. 61 00:03:51,740 --> 00:03:53,500 These can help with securing the cookie. 62 00:03:55,350 --> 00:03:57,590 The fifth parameter is the domain. 63 00:03:57,590 --> 00:04:00,345 This will allow you to control which domain and 64 00:04:00,345 --> 00:04:02,640 subdomains can access the cookie. 65 00:04:02,640 --> 00:04:05,590 The sixth parameter will allow us to set a cookie 66 00:04:05,590 --> 00:04:08,790 that is only available over a secure connection. 67 00:04:08,790 --> 00:04:15,620 The seventh and final parameter will allow us to set the cookie to HTTP only. 68 00:04:15,620 --> 00:04:18,180 This means that the cookie won't be accessible 69 00:04:18,180 --> 00:04:21,320 by scripting languages such as JavaScript. 70 00:04:21,320 --> 00:04:22,740 Let's go back to workspaces. 71 00:04:25,410 --> 00:04:27,407 Let's add a redirect to the welcome page. 72 00:04:33,525 --> 00:04:36,982 Now we can update the link on our story page to save the story. 73 00:04:45,996 --> 00:04:48,584 Our story is ready to be saved to a cookie, but 74 00:04:48,584 --> 00:04:50,982 we have no way of reading that cookie yet. 75 00:04:50,982 --> 00:04:55,420 In the next video, we'll read the cookie and use that data to rewrite the story.