0% found this document useful (0 votes)
49 views

How Session Work

A session allows information to be stored and accessed across multiple pages. It works by assigning a unique ID stored in a cookie on the client side and in a file on the server side. If the cookie ID matches the file ID, the session variables stored in the file are available on the page and can be accessed through the $_SESSION global variable. Session variables can be set, accessed, unset for an individual variable, or all variables can be destroyed by ending the session.

Uploaded by

guru11223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

How Session Work

A session allows information to be stored and accessed across multiple pages. It works by assigning a unique ID stored in a cookie on the client side and in a file on the server side. If the cookie ID matches the file ID, the session variables stored in the file are available on the page and can be accessed through the $_SESSION global variable. Session variables can be set, accessed, unset for an individual variable, or all variables can be destroyed by ending the session.

Uploaded by

guru11223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Session

A session is a way to store information (in variables) to be used across multiple pages.
How Session Works
Store a Cookie on Client Machine
Session
session_start( ) If NO Cookie_Name – PHPSESSID
available ? Content – SessionID

If Yes
Store a file on Server
Name – SESS_SessionID
Content – Session_Variable
It matches PHPSESSID Cookie’s
Session ID with Server’s File
Session ID: 80ibbfgbh7cl6lakgg87dlhv7v
If both match

All Session Variables are available


Starting Session
session_start() - It creates/starts a session or resumes the current one based on a session
identifier passed via a GET or POST request, or passed via a cookie.
This function first checks if a session is already started and if none is started then it
starts one. When It fails to start the session, returns FALSE and no longer initializes
$_SESSION.
Syntax:- session_start ( );

Note: The session_start() function must be the very first thing in your document. before any
HTML tags.
Set Session Variables
Session variables are set with the PHP global variable $_SESSION. These
variables can be accessed during lifetime of a session.

$_SESSION[‘username’] = ‘geekyshows’;
$_SESSION[‘password’] = ‘geek’;
$_SESSION[‘time’] = time();
$_SESSION[‘cart’] = $number;
Get/Access Session Variables
Session variables are stored in the PHP global variable $_SESSION.

$_SESSION[‘username’];
$_SESSION[‘password’];
$_SESSION[‘time’];
$_SESSION[‘cart’];
Unset Session Variable
• unset($_SESSION[‘varName’]) – This is used to
free/unset/unregister a session variable.
Ex:- unset($_SESSION[‘username’])

• session_unset() – This is used to free/unset/unregister all


session variables currently registered. It returns TRUE on
success or FALSE on failure.
Ex: - session_unset( )
Destroy Session
• session_destroy() – It destroys all of the data associated with
the current session. It does not unset any of the global
variables associated with the session, or unset the session
cookie. To use the session variables again, session_start() has
to be called.
Ex: - session_destroy ( );

You might also like