PHP Sessions



PHP Sessions

Session is used to store values across multiple pages of an entire website.

If we want to store some value that will be used in entire website then use a PHP session.

In other words session means the period of time the user login to system and exit from the system. when user login to system the user session start and when user logout from the system the session ended.Session variable generate unique id for each user of entire site. We can uniquely identify all users of website by using session.

  • The PHP session variable store information for a single user and its available to all website pages of entire website.
  • The session automatically end when browser windows closed.

The main functionality  of session

  • Session_start() – start the session.
  • $_SESSION[“variable”] – Store value to and retrieve value from session.
  • unset($_SESSION[“variable”]) – unset the single session variable.
  • session_destroy() – destroy all the session variable

PHP session start

PHP session is started using the php session_start() function.
The fuction session_start() call at the beginning of the page, means before the HTML start tags.

For store value in a session we use PHP global variable : $_SESSION.

PHP Session Syntax

session_start();
$_SESSION[“val”]=”meera”;

Example of session in PHP

Lets take an example to more understand about how to start session, store value in session and retrieve value from session in PHP.

Here, we create two php form, in first form we enter name in textbox and store that textbox value in session on button click event. After we go to on second php form retrieve that previous page textbox value on this second page using session.
The first page “firstexample.php” design with one textbox along with a button control like :

<?php
session_start();
?>
<html>
<head>
<title>PHP Session Example</title>
</head>
<body>

<FORM method="POST" action="firstexample.php">

Enter Name : <input type="text" name="name"> <br/>

<br/>
<input type="submit" name="Submit1" value="Session">

</FORM>

<?php
if(isset($_POST['Submit1']))
{ 
$_SESSION["sname"]=$_POST["name"];
}
?>

</body>
</html>

The sesond page “example.php” php form look like :

<?php

session_start();

echo "The user name = ". $_SESSION["sname"];

?>

The output of session example is :

PHP Session
PHP Session Example

In above example on first page we set $_SESSION[“sname”]=$_POST[“name”], the textbox value stores in session[“sname”] and on second page we retrieve the session[“sname”] value and display the value using echo statement.


PHP session Destroy

PHP session destroyed by using session_destroy() function. This function destroy all session of entire site.
If we want to destroy single session variable then we use unset() function.

Example of unset() session

<?php

unset($_SESSION["sname"]);

?>

Above code unset the single session variable $_SESSION[“sname”].

Example of session destroy

<?php

session_destroy();

?>

Above function will destroy all the session variable.

Leave a Reply

Your email address will not be published. Required fields are marked *