Session management is a mechanism to maintain state about a
series of requests from the same user across some period of time. That is, the
term "session" refers to the time that a user is at a particular web
site. The problem is that HTTP has no mechanism to maintain state. Individual
requests aren't related to each other. The Web server can't easily distinguish
between single users and doesn't know about user sessions. Session management
refers to the way that associate data with a user during a visit to a Web page.
A PHP session variable is used to store information about,
or change settings for a user session. Session variables hold information about
one single user, and are available to all pages in one application. I.e. A
session is a way to store information (in the form of variables) to be used
across multiple pages. Unlike a cookie, specific variable information is not
stored on the user’s computer. It 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.
Start Session:
Before you can store user information in your PHP session,
you must first start up the session. session_start() function is used for start
session into PHP page.
Syntax:
?php
// Start session
session_start();
?>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
Destroying Session:
There are basically two functions which is for destroying
Session, first one is unset() function and second one is session_destroy() .
The unset() function is used to free specified session
variable.
Example: Using unset function
<?php
// Start Session
session_start();
// check session value has been set or not
$_SESSION['TestUnset'] ="Unset";
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>PHP Session</title>
</head>
<body>
<div id
="viewContainer">
<h1> This is session page testing </h1>
<?php
// dispaly number of
visitor in
page
echo 'Session Value is : '.$_SESSION['TestUnset']."</br>";
unset ($_SESSION['TestUnset']);
echo 'Session Destroyed'."</br>";
echo 'Now Session Value is : '.$_SESSION['TestUnset'];
?>
</div>
</body>
</html>
Example: using session_destroy function
<?php
// Start Session
session_start();
// check session value has been set or not
$_SESSION['TestUnset'] ="Unset";
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>PHP Session</title>
</head>
<body>
<div id
="viewContainer">
<h1> This is session page testing </h1>
<?php
// dispaly number of
visitor in
page
echo 'Session Value is : '.$_SESSION['TestUnset']."</br>";
session_destroy() ;
echo 'Session Destroyed'."</br>";
echo 'Now Session Value is : '.$_SESSION['TestUnset'];
?>
</div>
</body>
</html>