Pass information Using SESSION variable


What is Session?
A session variable is a temporary variable that is created and stored at server side to uniquely identify each user on website.
A session is created for each user when he or she logs on to the web application and it remain in existence until he or she logs out.
How to Start Session?
In order to create or access session variable you need to start the session at the starting of script using session_start () function.
As you start session using session_start () function a unique identification number UID is generated for that user to uniquely identify the user.
How to Create Session variable?
A session variable can be created using following syntax:
$_SESSION [‘VariableName’] = Value;
Example:
$_SESSION [‘UserName’] = “ABC”;

You can use isset () function to determine weather the session variable with specific name exist or not as shown below:
If (isset ($_SESSION [‘SessionName’]))
{
echo $_SESSION [‘SessionName’];
}
How to Destroy Session?
A session variable can be destroyed from the server using unset () function or using session_destroy () function.
For Example:
unset ($_SESSION [‘SessionName’]);
Or
session_destroy ();

Example:
Step 1: Create a file named Session1.php that accepts username and Password from the user.

     

<html>
<head>
<title>Use of Session</title>
</head>
<body>
<form name="Form1" method="POST" action="Session2.php">
Enter Your Name:
<input type="text" name="UserName">
<br>
Enter Your Password:
<input type="password" name="Password">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Step 2: Create a file named Session2.php which authenticate the user and creates a session variable that stores username and redirects user to session3.php file.

<?php
session_start ();
$Name = $_POST['UserName'];
$Pass = $_POST['Password'];
if ($Name=="Hemant" and $Pass=="Oza")
{
$_SESSION[‘UserName’]=$Name;
header("location:Session3.php");
}
else
{
echo "Wrong username and password";
}
?>

Step 3: Now create a file named Session3.php which checks if the session variable exists for the username or not. If it exists then it will display username stored in the session otherwise it will redirects user to session1.php file.

<?php
session_start();
?>
<html>
<head>
<title<Use of Session</title>
</head>
<body>
<form name="Form1" method="POST" action="Session4.php">
<?php
if (isset($_SESSION['UserName']))
{
echo "Welcome ".$_SESSION['UserName'];
?>
<input type="submit" name="submit" value="Logout">
<?php
}
else
{
header("location:Session1.php");
}
?>
</form>
</body>
</html>

Step 4: Create file named session4.php that destroy all the session variables and redirect user to the session1.php file.

<?php
session_start();
session_destroy();
header("location:Session1.php");
?>

Step 5: Now run the session1.php file it will display following:

Pass Data Using Session

Step 6: Enter username and password and click on submit button. If you enter correct username and password then it will display username and Logout button as shown below:
Pass Data Using Session

Step 7: As you click on Logout button it will destroy session variable and redirect you to session1.php file. If you directly try to run session3.php file without entering username and password then it will redirects you to session1.php file.

Download Projects


Download Programs