Session In PHP



Suppose you are working on the PHP application and you want to store the user information for the future use, Here we use the Session, Session is used to store the user information on the server for the future use. However this storage is temporary type and it is flushed out when site is closed. In PHP session can be start by using the session_start() function, it is predefined functions in PHP.

Another Definition of the PHP Session : Sometimes we need to pass user data from one page to another, then we can do it by using the PHP sessions, it stores all the user information on the server for the future use. In this post I am trying to cover the points related to the PHP session.

Starting Session :

For using and storing the user information, we must have to start the session by using the PHP session_start(). It should place at the very beginning of PHP code to start a PHP session.

<?php
	session_start(); // For starting the PHP session! 
?>

This code will register the users session with the server, and allows you to start saving the user information on the server. Now you can assign a unique id to the user for the user session.

Storing the values into the Session :

For storing values into the session use the $_SESSION[”] PHP syntax.

Syntax are :

<?php

session_start();
$_SESSION['userName'] = ewa; // store session data
echo "User Name = ". $_SESSION['userName']; //retrieve data

?>

The above example is a very basic concepts of storing and retrieving data stored into a session. The useName value “ewa” is stored into the session and now you can display this value on the other page by using the $_SESSION.

Now Using the PHP isset function with the Session:

<?php
session_start();  
	if(isset($_SESSION['userName'])){
		echo $_SESSION['userName']; // if session is start and it stores the userName
	}
	else
	{
		echo "You are not logged in";
	}

	echo "User Name = ". $_SESSION['userName']; 
?>

In the above example I have used the isset() to match that the user is logged in and the user name which I am using to store into the session. It will display the User Name if the user is logged in other wise it will display the message that You are not logged in.

Ending/Destroying a Session:

While the data stored into the Session is temporary and it does not requires that you clean every time but it is requires for the online trasaction type application so that no one can misuse it. It is also good code practice and will avoid having a huge amount of raw session data stored on the server.

For deleting the session value,use the unset() function:

<?php
	session_start();
	unset($_SESSION["userName"]);// delete the userName value
?>

unset() is used to delete the single session values like the userName, for deleting the multiple session values use the session_destroy() PHP function:

<?php
	session_start();
	session_destroy();// terminate all the session
?>

want to know differences between unset and unlink php function click here
This is all about the PHP Session, Hope this would be helpfull, Thanks and Enjoy the PHP coding.