Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP Sessions


May 11, 2021 PHP


Table of contents


PHP Sessions

Session Chinese the translation of the word "session" and its original meaning is a series of actions/messages that begin and end.

The PHP session variable is used to store information about a user session, or to change the settings for a user session. The Session variable stores information for a single user and is available for all pages in the application.


PhP Session variable

When you work on an application on your computer, you open it, make some changes, and then close it. I t's a lot like a conversation. T he computer knows who you are. I t knows when you are opening and closing the application. However, a problem arises on the Internet: because the HTTP address cannot be maintained, the Web server does not know who you are and what you have done.

PHP session solves this problem by storing user information on the server for subsequent use (e.g. user names, purchases, etc.). H owever, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.

Session works by creating a unique iD (UID) for each guest and storing variables based on that UID. The UID is stored in a cookie or conducted via a URL.


Start PHP Session

Before you can store user information in PHP session, you must first start the session.

Note: session_start function must be before the label:

<?php session_start(); ?>

 <html>
 <body>

 </body>
 </html> 

The code above registers the user's session with the server so that you can start saving user information and assigns a UID to the user session.


Stores the Session variable

The correct way to store and get back the session variable is to use PHP $_SESSION variable:

 <?php
 session_start();
 // store session data
 $_SESSION['views']=1;
 ?>

 <html>
 <body>

 <?php
 //retrieve session data
 echo "Pageviews=". $_SESSION['views'];
 ?>

 </body>
 </html> 

Output:

Pageviews=1

In the following example, we created a simple page-view counter. T he isset() function detects whether the "views" variable has been set. I f the "views" variable is set, we add up the counter. If "views" does not exist, create the "views" variable and set it to 1:

 <?php
 session_start();

 if(isset($_SESSION['views'])){
     $_SESSION['views']=$_SESSION['views']+1;
 }else{
     $_SESSION['views']=1;

}
 echo "Views=". $_SESSION['views'];
 ?> 


Destroy Sessions

If you want to delete some session data, you can use the unset() session_destroy () function.

The unset() function is used to release the specified session variable:

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

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

<?php
 session_destroy();
 ?> 

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

We'll introduce you to PHP E-Mail in the next section.