>> PROFESSOR: Let's write a program that keeps track of how many times a user
has visited a web page, thereby demonstrating how you can use PHP's
feature known as a session.
Let's get started by first taking a look at the HTML that I've prepared in
advance here.
Notice that I have put in the body of this page the statement, you have
visited the site this many times, whereby this many is going to be the
result of outputting a variable that's apparently called dollar sign counter.
>> Now, up at the top of this file I've left some space between an open tag
and a closed tag for some PHP code.
Let's now start a session by specifying the following.
Session_start and calling that function.
What that simply does is inform PHP that I would like to begin using
Session so that the super global, dollar sign underscore session, is
available to me.
And PHP and the web server will, therefore, magically take care of how
that variable is actually implemented back and forth between client and
server, somehow using cookies.
>> But in code, what I next want to do is something like the following.
First, let's check if there's already a variable set in my session.
If is set dollar sign underscore session quote unquote counter.
In other words, I'm going to use the session super global to store a key,
also called counter, whose value is going to be the total number of times
that the user has visited my site, and I'll grab that value as needed and put
it in my local variable, dollar sign counter, in order to
display that value.
>> Next, if that key is indeed set with a value, I'm going to go ahead and grab
that value with dollar sign counter gets dollar sign underscore session
open bracket quote unquote counter close bracket semicolon.
If that variable is not set, let's simply initialize counter with the
value zero.
Meanwhile, no matter what happens up there, let's update dollar sign
underscore session quote unquote counter to be whatever this local
variable is plus 1.
>> I claim, now, by way of this branch and a bit of arithmetic, I am going to
start counting how many times the user has visited this page.
Let's take a look.
Let's save the file and open it up in a browser.
>> Let's visit http://localhost/counter/php.
This is the first time I'm here so, indeed, I visited the site zero times.
But let's now reload, let's now reload, let's now reload, and as
expected, I've now visited this site not zero, but three times.