Working with PHP Sessions


PHP Sessions

A session is a way to store information (in the form of variables) to be used across multiple pages.

Or
 
The PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.

Starting with PHP session

For starting a php session use the builtin function session_start(). This function is used in the between the php tags, as follow:

<html>
<
head>
<
title>Session</title>
</
head>
<
body bgcolor="orange">
<center>
<
h3> Starting up a session </h3><hr>
<
p> session_start() </p>
<?php
session_start(); // start up your PHP session!
?>
</center>
</
body>
</html>

Save it as session1.php

Output of the above script

To run the code Open the XAMPP server an start the services like Apache and MySQL. Open the browser and type: http://localhost/yourfoldername/session.php  

s1.gif

Storing a session variable

In this section we will use the
correct way to store and retrieve session variables is to use the PHP $_SESSION variable:

PHP script for storing session

<html><head><title></title></head>
<
body bgcolor="cyan">
<center>
<
h3> Storing the session in the variable</h3><hr>
<?php
session_start();
$_SESSION[
'views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>
</center>
</
body>
</html>

Save it as seevar.php

Output of above script

To run the code, Open the XAMPP server and start the services like Apache and MySQL. Open the browser type: http://localhost/yourfoldername/session.php  

s2.gif

PHP session with isset() function

Here in this section, session is created with the help of isset().
W
e create a simple page-views counter. The isset() function checks if the "views" variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't exist, we create a "views" variable, and set it to 1

PHP script

<html><head><title></title></head>
<
body bgcolor="yellow">
<center>
<
h3> Storing the session in the variable isset() </h3><hr>
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION[
'views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
</center>
</body>
</html>

Save it as isset.php

Output

To run the code, Open the XAMPP server and start the services like Apache and MySQL. Open the browser type: http://localhost/yourfoldername/session.php 

s3.gif

If you hit the same URL again and again the counter for session will incremented.

s4.gif

s5.gif

Destroying a PHP session

Destroying a session is also a very important part so if you wish to delete some session data, you can use the unset() or the session_destroy() function.The unset() function is used to free the specified session variable:

<?php
unset($_SESSION['views']);
?>

You can also completely destroy the session by calling the session_destroy() function:

<?php
session_destroy();
?>

Note: session_destroy() will reset your session and you will lose all your stored session data.

Conclusion: You can use the above given session concepts for your php application. Thanks

Up Next
    Ebook Download
    View all
    Learn
    View all