PHP Session

PHP SESSION VARIABLE

Session variable is a special type of array that holds information for an individual user with again unique session ID, like sending information from one web page to another or many pages using a unique identity by session ID for the individual user. It is used to persist (hold on) state information between page requests.

session variable automatically shuts down when PHP finishes executing a script, but we can shut down using the session_write_close()  function.

Session Variable function’s list

Function Definition
session_start It creates a session or resumes the current one based on the session ID identifier that passed through the GET or POST request.
session_status Return the current status of the session variable.
session_id It is used to get or set the session ID for the current session.
session_abort It discards changes to the session array and finishes the session.
session_destroy It destroys all data associated with the session variable.
session_reset It reinitializes the session variable with its original value.
session_register Register one or more global variables (defined outside the function) with the current session.
session_unset Free all session variables that have been registered
session_write_close It is used to write session data and end the session.

What is $_SESSION in PHP

$_SESSION is an associative array containing session variables available to the current script. We can apply a session variable like this.

PHP Session Start

<?php

session_start(); //It should be mentioned first line of the script after PHP.

$_SESSION[‘variable_name’]=value;

?>

Destroying php session

A PHP session can be destroyed using the session_destroy() function. A single call to this function can destroy all session variables of the current running PHP script.

If we want to destroy a 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 a login page and selecting fruit, and displaying it using a session

For example, we need to create three .php files

  • 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>

Example of php session variable
Example of php session variable
login form in php

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!”;

}

?>

Session variable in PHP

display.php

<?php

session_start();

$fruit=$_POST[‘fruit’];

echo $_SESSION[‘uid’].” you have selected $fruit”;

?>

Example of php session variable
Example of php session variable
session start method in PHP

PHP Loops