SESSION variable

The session variable is used to temporally store information about a single user and it  is still available for all pages of the web site until the user leaves the site. The session is stored in the server, not in the client machine. Before you can use a session variable to store information, you must start the session.

Example:
<html>
<head>
<title>Session storing information</title>
<?php
session_start(); //start session
$_SESSION['loginname'] ="username";
//loginname is the unique ID of the session. This session stores the "username" string.
?>
</head>
<body>
</body>
</html>

To get the information from the session, you can write as shown below:
<html>
<head>
<title>Get information from the session</title>
<?php
echo $_SESSION['logname']; //get information from the session
?>
</head>
<body>
</body>
</html>

To destroy the session when it is no longer to use, you can call the session_destroy() method.
Example:
<html>
<head>
<title>Destroying the session</title>
<?php
if(isset($_SESSION['logname']))
    session_destroy(); 
?>
<head>
<body>
</body>
</html>

Note: isset() function is used to see whether the session has a value stored in it. By using the session_destroy() function you will clear all the sessions that exist in the web site. If you want to clear a specific session you need to use the unset() method. To clear only one session used above you can write as shown below:
unset($_SESSION['logname']);



HTML Comment Box is loading comments...



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.