hi everyone today I'm just going to show
you how to use this session variable in
PHP and I'm going to do it by way of
demonstration in this simple kind of
system but I have written in PHP I just
want to put in a page count that's gonna
count every single page on a user's
visit first things first I'm going to go
to the code and with the session
variable in PHP it always starts with
using this session start function at the
top of every page so that's what's
important
so simply by putting in that one
statement that one function called
session start we have now access to that
session variable in PHP just putting in
this session starts function call
doesn't actually do anything to the call
itself for example if I go back to my
page here press refresh we don't see any
major difference but by putting in this
session start function call I have
access to the session variable we need
to discuss what that session variable is
it is an array that is built into PHP
that keeps track of a user as it goes
through a particular site visit and you
can add into that rate array anything
that you like and come back and access
that item at any other stage in that one
session so for example if I wanted to
use a page count functionality and I
want to keep track of every page or
every visit to this one page that a user
has and keep an incremental count going
this is however do it
to access the session variable this is
the syntax and then I want to access a
particular item within the session
variable using an associative array
label and what I'm going to use there is
just an item called page count and then
I can come along and just increment that
page count every time that somebody
comes to this one page
like so and so every time that this page
is loaded the session variable gets
called and we get whatever is in page
count at the moment we just add one to
it add one to it and then assign that
back into page count in the session
variable let's see if that makes any
difference in our page at the moment I'm
just gonna save that PHP come back and
just refresh this page now as you can
see nothing happened nothing seems to be
happening different but in actual fact
the session variable that page count
item is being incremented every time
better refresh this page it's just that
we're not outputting the result anywhere
so that's my next step I'm gonna go down
here just to show you that this page
count variable is getting incremented
just inside my main content I am going
to put in PHP call and I'm going to call
the session variable access the page
count item and echoed that item out
let's say that and let's see how it goes
so I've got my number here five press
refresh goes to six goes to seven goes
to H every time I'm refreshing again I'm
just incrementing that page count item
and it's echoing it out into the main
part of the page we can see how we can
access that session variable and how we
can use it to keep track of a page count
but what we've done so far is just on
one page I want to keep track of the
page count as the user goes through all
the different pages on the website so go
back to my code that's reason out how we
might try and approach that we've seen
accessing this session variable and how
it can be used to show an incrementing
page count but I'd like this to happen
across my whole website rather than just
on one page so I need to get this code
here and just put it at the top of every
page on my website I could just do
exactly what I have there take that PHP
code block and just paste it in or just
to make it a little bit more
maintainable I would like to have that
just in one single file and require it
into the top of the PHP files so I've
got a pipe page here or a file in PHP
called session dot PHP doesn't have to
be called session I just picked that
name because that's
while it leads to and in this session
that PHP file I'm just gonna paste in
the exact same two statements that I've
seen before so those are the two
different statements student exactly the
same as what we had before I'm just
going to go back to the previous page
now and just show you how we tidy that
up so instead of calling these two
statements here I'm just gonna require
in session dot PHP like so and this code
has to go up at the top of every page
just one thing of note remember that
your session start function has to be at
the very first item inside the HTML page
even right before the HTML or doctype
declaration it has to go up at the very
top it's required of all the
navigation links that I have on this
system and I'm just gonna put my page
counter in there so let's just took a
look at part up don't they what navbar
and here we go just down the bottom of
my navbar I've put in a PHP code block
and exactly the same code as I had
before echoing out page count and just
saying what it is let's save that let's
see the results so as I move through my
different pages now it's keeping track
of my page count every time accessing
the session variable incrementing the
page count item within that variable
within that array and I'm getting my
page count getting implemented every
time so that's a brief introduction to
the session variable and PHP how to
access it how to add tuition how to read
from