PHP Session is a very important as we generally use the session to maintain data throughout the application in temporary form.The session can be used to store various types of data so it has many uses, also used to store the login information of the user, shopping cart data, or some temporary data.
What is a Session?
The session is the variables which is used to move data between pages of hiding from the others.A PHP session allows an application to store information for the current session.A session is identified by a unique session ID.The session is used to store data temporarily.
Session Usage
The first step in a script that is to let PHP know you want to use the session feature by calling the function session_start() which takes no argument.
So, To initialize the session with session_start() at the beginning of the page to use session:
session_start();
To create the session,We need to use $_SESSION array.The $_SESSION array is one of the superglobal variables. We do not need a function or class. You can use the $_SESSION superglobal array which will store anything that you want to retrieve again in any page of application in the same session,we can do here.
1 2 3 4 5 6 7 8 | <?php session_start(); /*Session operations*/ $_SESSION["session_name"]="session value"; /*Created a new session*/ ?> |
How to get the Session Value
Again, using the $ _SESSION superglobal array in our page. index.php page will direct you to our page.
The code can be as simple as the following example:
1 2 3 4 5 6 7 8 9 10 | <?php session_start(); /*Session operations*/ $_SESSION["session_name"]="session value"; /*Created a new sesson*/ /*index.php redirection*/ ?> |
Now,Lets see the index.php code:
1 2 3 4 5 6 7 8 | <?php session_start(); /*Start the Session Process*/ echo $_SESSION["session_name"]; /*Output : session value */ ?> |
Although,you can get the session in different files on different pages without the knowledge of the browser version from the array name of $_SESSION.The $_SESSION array is a way of sharing information between pages.
How to delete Session
There are many ways to delete or destroy your session.First, let’s use the unset function.
- unset()
The unset function is very easy to remove from the array, just like other arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php session_start(); /*Session operations*/ $_SESSION["session_name"]="session value"; /*Session display*/ echo $_SESSION['session_name']; /*remove the session value */ unset($_SESSION['session_name']); echo $_SESSION['session_name']; /* ERROR !!!!! because we have removed the element name*/ ?> |
- session_destroy()
If you want to delete all the elements in the array,session_destroy() is useful.session_destroy() function clear the session by setting the $_SESSION superglobal to an empty array as shown in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php session_start(); /*Session operations*/ $_SESSION["session_name"]="session value"; $_SESSION["session_name1"]="session value1"; $_SESSION["session_name2"]="session value2"; /* Let’s display session */ foreach($_SESSION as $key => $value){ echo $key. " => ".$value."<br/>"; } session_destroy(); ?> |
A session ID of browser may still be the same after this function call so if you call session_start() again,you may get value again so it is better to use session_regenerate_id() after destroying session:
1 2 3 4 5 6 7 | <?php session_destroy(); session_regenerate_id(); session_start(); ?> |
- session_unset():
session_unset() function takes no argument and release all variables in the session
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php session_start(); /*Session operations*/ $_SESSION["session_name"]="session value"; $_SESSION["session_name1"]="session value1"; $_SESSION["session_name2"]="session value2"; /* Let’s display session */ foreach($_SESSION as $key => $value){ echo $key. " => ".$value."<br/>"; } session_unset(); ?> |
- $_SESSION = array();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php session_start(); /*Session operations*/ $_SESSION["session_name"]="session value"; $_SESSION["session_name1"]="session value1"; $_SESSION["session_name2"]="session value2"; /* Let’s display session */ foreach($_SESSION as $key => $value){ echo $key. " => ".$value."<br/>"; } /*Create session again*/ $_SESSION = array(); /*Again defining a set of variables that have been cleaned to empty which delete all values*/ echo $_SESSION['session_name']; /*ERROR !!!!! because we have removed all the elements.*/ ?> |
How to Update Session
Process we perform to update the normal array, the same way you can update to session array.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php session_start(); /*Session operations*/ $_SESSION["session_name"]="session value"; /*Session display*/ echo $_SESSION['session_name']; /*Now assign new value*/ $_SESSION['session_name'] = "Creativedev"; echo $_SESSION['session_name']; /* OUTPUT: Creativedev*/ ?> |
It’s that simple. That’s all there is to it.How are you using session in your PHP application? I’d love to hear about your experiences in the comments.