1 00:00:00,000 --> 00:00:00,130 2 00:00:00,130 --> 00:00:02,980 >> PROFESSOR: Let's write a program that keeps track of how many times a user 3 00:00:02,980 --> 00:00:06,390 has visited a web page, thereby demonstrating how you can use PHP's 4 00:00:06,390 --> 00:00:08,320 feature known as a session. 5 00:00:08,320 --> 00:00:11,740 Let's get started by first taking a look at the HTML that I've prepared in 6 00:00:11,740 --> 00:00:12,800 advance here. 7 00:00:12,800 --> 00:00:16,620 Notice that I have put in the body of this page the statement, you have 8 00:00:16,620 --> 00:00:20,770 visited the site this many times, whereby this many is going to be the 9 00:00:20,770 --> 00:00:25,070 result of outputting a variable that's apparently called dollar sign counter. 10 00:00:25,070 --> 00:00:28,800 >> Now, up at the top of this file I've left some space between an open tag 11 00:00:28,800 --> 00:00:30,820 and a closed tag for some PHP code. 12 00:00:30,820 --> 00:00:34,380 Let's now start a session by specifying the following. 13 00:00:34,380 --> 00:00:37,790 Session_start and calling that function. 14 00:00:37,790 --> 00:00:41,150 What that simply does is inform PHP that I would like to begin using 15 00:00:41,150 --> 00:00:45,040 Session so that the super global, dollar sign underscore session, is 16 00:00:45,040 --> 00:00:45,820 available to me. 17 00:00:45,820 --> 00:00:49,590 And PHP and the web server will, therefore, magically take care of how 18 00:00:49,590 --> 00:00:53,260 that variable is actually implemented back and forth between client and 19 00:00:53,260 --> 00:00:55,150 server, somehow using cookies. 20 00:00:55,150 --> 00:00:58,690 >> But in code, what I next want to do is something like the following. 21 00:00:58,690 --> 00:01:03,250 First, let's check if there's already a variable set in my session. 22 00:01:03,250 --> 00:01:09,940 If is set dollar sign underscore session quote unquote counter. 23 00:01:09,940 --> 00:01:13,690 In other words, I'm going to use the session super global to store a key, 24 00:01:13,690 --> 00:01:17,180 also called counter, whose value is going to be the total number of times 25 00:01:17,180 --> 00:01:20,790 that the user has visited my site, and I'll grab that value as needed and put 26 00:01:20,790 --> 00:01:23,630 it in my local variable, dollar sign counter, in order to 27 00:01:23,630 --> 00:01:25,190 display that value. 28 00:01:25,190 --> 00:01:29,850 >> Next, if that key is indeed set with a value, I'm going to go ahead and grab 29 00:01:29,850 --> 00:01:35,880 that value with dollar sign counter gets dollar sign underscore session 30 00:01:35,880 --> 00:01:40,130 open bracket quote unquote counter close bracket semicolon. 31 00:01:40,130 --> 00:01:43,160 If that variable is not set, let's simply initialize counter with the 32 00:01:43,160 --> 00:01:44,600 value zero. 33 00:01:44,600 --> 00:01:48,700 Meanwhile, no matter what happens up there, let's update dollar sign 34 00:01:48,700 --> 00:01:52,400 underscore session quote unquote counter to be whatever this local 35 00:01:52,400 --> 00:01:54,770 variable is plus 1. 36 00:01:54,770 --> 00:01:58,930 >> I claim, now, by way of this branch and a bit of arithmetic, I am going to 37 00:01:58,930 --> 00:02:01,480 start counting how many times the user has visited this page. 38 00:02:01,480 --> 00:02:02,240 Let's take a look. 39 00:02:02,240 --> 00:02:04,240 Let's save the file and open it up in a browser. 40 00:02:04,240 --> 00:02:07,190 41 00:02:07,190 --> 00:02:13,630 >> Let's visit http://localhost/counter/php. 42 00:02:13,630 --> 00:02:17,120 This is the first time I'm here so, indeed, I visited the site zero times. 43 00:02:17,120 --> 00:02:22,060 But let's now reload, let's now reload, let's now reload, and as 44 00:02:22,060 --> 00:02:25,300 expected, I've now visited this site not zero, but three times. 45 00:02:25,300 --> 00:02:26,816