Вы находитесь на странице: 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 ( );

Вам также может понравиться