1 00:00:00,420 --> 00:00:05,420 A session is a term referring to a user's time browsing a website. 2 00:00:05,420 --> 00:00:09,640 It's meant to represent the time between a user's first page view on a particular 3 00:00:09,640 --> 00:00:14,510 site until the time when that user is finished at that site. 4 00:00:14,510 --> 00:00:18,430 It's impossible to know for sure when a user is done with a site. 5 00:00:18,430 --> 00:00:23,060 Instead, the session is terminated in one of two ways. 6 00:00:23,060 --> 00:00:27,800 Either the user closes the browser, or the session times out when a request 7 00:00:27,800 --> 00:00:30,690 has not been made within a certain amount of time. 8 00:00:30,690 --> 00:00:36,120 By default, a PHP session is set to timeout after 1440 seconds, 9 00:00:36,120 --> 00:00:37,040 which is 24 minutes. 10 00:00:38,480 --> 00:00:42,970 PHP offers us the ability to set variables that are available for 11 00:00:42,970 --> 00:00:45,360 the duration of a user's session. 12 00:00:45,360 --> 00:00:47,760 These are called session variables. 13 00:00:47,760 --> 00:00:52,155 Let's go back into workspaces and set some session variables based on user input. 14 00:00:53,190 --> 00:00:55,310 Open play.php. 15 00:00:55,310 --> 00:00:58,524 To access sessions, we first need to start the session. 16 00:00:58,524 --> 00:01:01,214 We use session_start. 17 00:01:02,501 --> 00:01:05,973 This should be called before anything is output to the browser, so 18 00:01:05,973 --> 00:01:09,073 it's usually best to place this call at the top of the file. 19 00:01:09,073 --> 00:01:14,183 Next, we check if a word has been posted if 20 00:01:14,183 --> 00:01:19,299 (isset($_POST['word'])). 21 00:01:22,465 --> 00:01:30,750 If so we can add a session variable, $_SESSION['word']. 22 00:01:30,750 --> 00:01:35,748 We can use the page variable to define which word this is, either by adding it to 23 00:01:35,748 --> 00:01:41,070 the variable name itself, +$page, or by making the session variable an array. 24 00:01:44,043 --> 00:01:50,033 Since we post to the page number after the word, we should use page-1. 25 00:01:52,931 --> 00:01:55,400 Then as always, filter user input. 26 00:01:58,302 --> 00:02:00,728 INPUT_POST. 27 00:02:01,964 --> 00:02:02,700 'Word'. 28 00:02:04,869 --> 00:02:07,804 FILTER_SANITIZE_STRING. 29 00:02:09,872 --> 00:02:13,780 To make sure our session is working, let's var_dump the session variable. 30 00:02:15,600 --> 00:02:17,450 Now, let's try out our game in the browser again. 31 00:02:27,517 --> 00:02:29,701 After we enter the word and push Submit, 32 00:02:29,701 --> 00:02:33,440 we can see that our session variable word is an array. 33 00:02:33,440 --> 00:02:36,190 The key of one equals our first word. 34 00:02:36,190 --> 00:02:41,542 Let's go back to workspaces and use these variables in the story before we continue. 35 00:02:41,542 --> 00:02:46,251 We can remove our var_dump and open story.php. 36 00:02:47,609 --> 00:02:50,800 At the top we have five word variables. 37 00:02:50,800 --> 00:02:53,520 Let's set these variables to the session variable. 38 00:02:53,520 --> 00:02:57,430 We start by adding a session_start to the top of the file. 39 00:02:57,430 --> 00:03:02,880 We also want to escape our user data so we use the function htmlspecialchars, 40 00:03:12,555 --> 00:03:15,379 _SESSION['word][1]. 41 00:03:35,170 --> 00:03:37,000 Let's finish running through our game now. 42 00:03:54,587 --> 00:03:57,731 Great, we keep track of each word the user enters and 43 00:03:57,731 --> 00:04:00,590 then use those words to write our story. 44 00:04:00,590 --> 00:04:02,980 There's one last thing that we should look at. 45 00:04:02,980 --> 00:04:05,360 That is, removing session variables. 46 00:04:05,360 --> 00:04:09,600 If the user chooses Play Again, we should clear the current session variables. 47 00:04:09,600 --> 00:04:14,509 Let's go back to play.php If they come 48 00:04:14,509 --> 00:04:18,400 to this page without a p parameter, we should clear the session variable. 49 00:04:20,380 --> 00:04:24,810 We can clear individual session variables just like we would any other variable, 50 00:04:24,810 --> 00:04:26,820 either setting them to an empty variable. 51 00:04:34,789 --> 00:04:38,080 Or calling the unset function on that variable. 52 00:04:45,353 --> 00:04:48,895 To unset all session variables at once, we should instead, 53 00:04:48,895 --> 00:04:51,125 use the function session_destroy. 54 00:05:00,175 --> 00:05:01,570 Now, let's go back to the browser. 55 00:05:03,120 --> 00:05:04,570 If we navigate to the welcome page. 56 00:05:06,819 --> 00:05:10,650 And then go back to the story, the session variables are still set. 57 00:05:10,650 --> 00:05:14,640 If we choose Play Again and then go back to our story, 58 00:05:15,640 --> 00:05:19,620 the errosr show us that the session variables are no longer available. 59 00:05:19,620 --> 00:05:23,660 This also shows us some more logic we should add, making sure that the required 60 00:05:23,660 --> 00:05:26,930 session variables are set before attempting to write the story.