You are on page 1of 6

What is a session?

A session is basically a way of storing variables and making them available across
multiple pages on your web site. This can be very useful as the user does not ever need
to see what is going on behind the scenes.
Definition: Sessionstart!" is used in #$# to initiate a session on each
#$# page. %t must be the first thing sent to the browser& or it won't work properly& so it's
usually best to place it right after the (?php tags. This must be on every page you intend
to use sessions on. )or more information read our sessions tutorial.
SessionStart!" is also known as: Start Session #$#.
*+amples: (?php
,, this starts the session
sessionstart!"-
,, this sets variables in the session
.S*SS%/01'test'23'testing'-
echo4Done4-
?5
What Are Session #arameters?
Session parameters& also called session variables& in the conte+t of a web
session are values that are available for the life of the session to web application.
The values are referenced by the web application by a defined name.
Session parameters are generally defined and managed by a parent web
application.
A session creates a file in a temporary directory on the server where
registered session variables and their values are stored. This data will be available to all
pages on the site during that visit.
The location of the temporary file is determined by a setting in the php.ini file called
session.savepath. 6ore using any session variable make sure you have setup this
path.
When a #$# script wants to retrieve the value from a session variable& #$#
automatically gets the uni7ue session identifier string from the #$#S*SS%D cookie and
then looks in its temporary directory for the file bearing that name and a validation can
be done by comparing both values.
A session ends when the user loses the browser or after leaving the site& the server will
terminate the session after a predetermined period of time& commonly 89 minutes
duration.
Starting a Session
A session is a way to store information !in the form of variables" to be used across
multiple pages. :nlike a cookie& specific variable information is not stored on the users
computer. %t is also unlike other variables in the sense that we are not passing them
individually to each new page& but instead retrieving them from the session we open at
beginning of each page.
;all this code mypage.php
(?php
,, this starts the session
sessionstart!"-
,, this sets variables in the session
.S*SS%/01'color'23'red'-
.S*SS%/01'si<e'23'small'-
.S*SS%/01'shape'23'round'-
print 4Done4-
?5
The first thing we do with this code& is open the session using sessionstart!". We then
set our first session variables !color& si<e and shape" to be red& small and round
respectively.
=ust like with our cookies& the sessionstart!" code must be in the header and you can
not send anything to the browser before it. %t's best to >ust put it directly after the (?php
to avoid potential problems.
So how will it know it's me? ?ost sessions set a cookie on your computer to uses as a
key... it will look something like this: 8@9A9BbeC@bbb9fafd8dDBEaBaBd@e@A. Then when
a session is opened on another page& it scans your computer for a key. %f t
here is a match& it accesses that session& if not it starts a new session for you.
$ow do % use a session?
$ere we will demonstrate a session in its simplest form& that is& setting a session
variable on a web page and recalling the value of it on a second page. We will begin by
creating a session variable called 'foo' and we shall assign the value of 'bar' to it. A
session variable looks like this..
.S*SS%/01'variable'2
We use the #$# super global .S*SS%/0 to hold it. ?ore on this later. /ur first page
we will call pageB.php and the code will look like this...
*+amples:
(?php
,, begin the session
sessionstart!"-
,, set the value of the session variable 'foo'
.S*SS%/01'foo'23'bar'-
,, echo a little message to say it is done
echo 'Setting value of foo'-
?5
Starting a #$# Session:
A #$# session is easily started by making a call to the
sessionstart!" function.This function first checks if a
session is already started and if none is started then it
starts one. %t is recommended to put the call to
sessionstart!" at the beginning of the page.
Session variables are stored in associative array
called .S*SS%/012. These variables can be
accessed during lifetime of a session.
The following e+ample starts a session then register a
variable called counter that is incremented each time
the page is visited during the session.
?ake use of isset!" function to check if session
variable is already set or not.
#ut this code in a test.php file and load this file many
times to see the result:
(?php
sessionstart!"-
if! isset! .S*SS%/01'counter'2 " "
F
.S*SS%/01'counter'2 G3 B-
H
else
F
.S*SS%/01'counter'2 3 B-
H
.msg 3 4Iou have visited this page 4.
.S*SS%/01'counter'2-
.msg .3 4in this session.4-
?5
(html5
(head5
(title5Setting up a #$# session(,title5
(,head5
(body5
(?php echo ! .msg "- ?5
(,body5
(,html5
The #$# code % am using to generate the session id is as follows :
#$# ;ode:
function getcartid!"F ,,Jenerate Session %d
if!isset!.S*SS%/0"" F
return .S*SS%/014cartid42-
H else F
sessionstart!"-
.S*SS%/014cartid42 3 sessionid!"-
return .S*SS%/014cartid42-
H
H

You might also like