PHP SESSION VARIABLE
Session variable is special type of array that holds information for individual user again unique session id like sending information from one web page to another or many pages using unique identity by session id for individual user. It is used to persist (hold on) state information between page requests.
session variable automatically shut down when PHP is finished executing a script, but manually we can do shut down using session_write_close() function.
Session Variable function’s list
Function | Definition |
session_start | It create a session or resumes the current one based on session id identifier that passed through GET or POST request. |
session_status | Return current status of session variable. |
session_id | It is used to get or set session id to the current session. |
session_abort | It discard changes of session array and finish session. |
session_destroy | It destroy all data associated with session variable. |
session_reset | It re-initialize session variable with original value. |
session_register | Register one or more global variables (defined outside the function) with the current session. |
session_unset | Free all session variable that currently has been registered |
session_write_close | It is used to write session data and end session. |
What is $_SESSION in php
$_SESSION is associative array containing session variables available to the current script. We can apply session variable like this.
PHP Session Start
<?php
session_start(); //It should me mention first line of script after php.
$_SESSION[‘variable_name’]=value;
?>
Destroying php session
A php session can be destroy using session_destroy() function. Single call of this function can destroy all session variable of current running php script.
If we want to destroy single session variable, we can use of unset () function to unset() session variable.
Here example of unset with php session
<?php
unset($_SESSION[‘counter’]);
?>
Creating and Working Session in PHP
How to use of session variable in php
Example of php session variable
with login page and selecting fruit and display it using session
For example we need to create three .php file
- login1.php
- trap.php
- display.php
Login1.php
<html>
<head<title>Login Page</title></head>
<body>
<form method=”post” action=”trap.php”>
User ID: <input type=”text” name=”uid”/><br/>
Password:<input type=”password” name=”pwd”/><br/>
<input type=”submit” name=”submit” value=”Login”>
</form>
</body>
</html>
trap.php
<?php
session_start();
$_SESSION[‘uid’]=$_POST[‘uid’];
$_SESSION[‘pwd’]=$_POST[‘pwd’];
if($_SESSION[‘uid’]==’php’ and $_SESSION[‘pwd’]==’1234′)
{
?>
<form method=”post” action=”display.php”>
Fruit choice for eat :<select name=”fruit”>
<option>Select</option>
<option>Mango</option>
<option>Banana</option>
<option>Orange</option>
</select>
<input type=”submit” name=”submit” value=”Select”/>
</form>
<?php
}
else
{
include(‘login1.php’);
echo “User or Password may be wrong!”;
}
?>
display.php
<?php
session_start();
$fruit=$_POST[‘fruit’];
echo $_SESSION[‘uid’].” you have selected $fruit”;
?>