PHP Session
PHP Tutorial

PHP Session

 

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

FunctionDefinition
session_startIt create a session or resumes the current one based on session id identifier that passed through GET or POST request.
session_statusReturn current status of session variable.
session_idIt is used to get or set session id to the current session.
session_abortIt discard changes of session array and finish session.
session_destroyIt destroy all data associated with session variable.
session_resetIt re-initialize session variable with original value.
session_registerRegister one or more global variables (defined outside the function) with the current session.
session_unsetFree all session variable that currently has been registered
session_write_closeIt 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>

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

?>

session start method in PHP

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment