Creating, destroying, and working with session in php

PHP sessions allow website applications khổng lồ store user information on the server. As a result, the data can be accessed from several pages throughout a website.

Bạn đang xem: Creating, destroying, and working with session in php

Sessions rely on an identifier lớn identify different users. It is, therefore, easy khổng lồ retrieve data about a particular user.

Unlike sessions, cookies store users’ information on their local computer.


To follow along, you need:

A basic understanding of PHP basic concepts.

Understanding PHP sessions

A PHP session stores user’s data that can be rendered across several pages of an application or website.

A quality session identifier or ID is used to identify a specific user. Therefore, when a session ID is null, it implies no session has been created yet. Hence, PHP is prompted lớn initiate one.

In this tutorial, we will learn how lớn start sessions & initialize variables shortly. We will make use of session variables ($_SESSION). This is a PHP superglobal. You can read more about PHP superglobals from here.

How lớn start a session

We need to store user data in session variables before they can be accessed across multiple website pages. Therefore, our first step is khổng lồ start a session by invoking a PHP function called session_start().

The session_start() function creates a new session, or restarts an existing one then generates a quality session ID for the user. This is mainly done through a GET or POST request. You can learn more about these request methods from here.

It is always important to place the session_start() function immediately after the tag at the beginning of your script. This ensures that all the required functionalities are accessible.

Let’s create a details.php file and then include the following code:

You should have the following output:

Successfully set the session variables.

Accessing a created session

Now that we already have our sessions in the preceding, let’s retrieve them lớn ensure that they were mix successfully.

Create an accessdetails.php file. We will use this tệp tin to access the previously mix session variables.

We will need to lớn use a conditional statement to access the required session variables. We will also require an isset() function to kiểm tra whether the session variables were set.

Since our sessions were phối in the details.php file, we should have the following output:

Hi Neema Muganga, glad to lớn know you enjoy writing too!
In case you misspelled the session variables, PHP may not recognize the data you are referring to and, therefore, return the else statement.


Sorry.. No such session variables set!
Ensure you place a semicolon at the end of a PHP statement lớn avoid syntax errors that prevent your code from running.

Xem thêm: test code php html

How to update a phối session

In some cases, we may want to change a session variable khổng lồ a different value.

In this step, we will replace the preset name variable in the details.php file, as shown below:

The above code will change your name và then display it in the browser.


You changed your name khổng lồ Liz Muganga!

Destroying a session.

The whole point of using sessions was khổng lồ store user’s data & make it accessible throughout the website application.

Therefore, the hệ thống can determine who is accessing the application using the chất lượng session identifier.

The session will be destroyed when one logs out or closes the browser window.

Functions we may use when destroying a session.

We can also use the following data lớn destroy the stored session variables:

unset() - This function destroys one particular session variable. It requires the target variable as a parameter.

session_destroy() - This function destroys all previously set session variables. It does not require any parameters.

The following code shows how to lớn destroy a PHP session:

From this analysis, I hope you have an idea of how the logout functionality is implemented in many applications.

Creating an application that implements sessions

Let’s put in practice what we have learned in the above

We will create a simple web application that uses PHP sessions. We will utilize Bootstrap styling khổng lồ save time.

The first step is lớn create a file and name it file1.php. In this file, showroom the following code:

//Starting the Sessionsession_start();//Accessing session variables$_SESSION<'name'> = 'John Doe';$_SESSION<'email'> = 'john';?>html lang="">head> meta charset=""> meta http-equiv="" content=""> meta name="" content=", initial-scale=1.0"> title>PHP Projecttitle> links rel="" href=""> link href="
When you navigate lớn your browser, the web page should look, as shown below:

When you view the phầm mềm in your browser, you should have the following output:


As stated, when you click the button in file1.php, you will be redirected lớn a different page, as demonstrated below:


This page retrieves the variables phối in file1.php & displays them on the browser.

We can destroy our sessions by closing the browser or logging out of an application.

We can also delete sessions programmatically using session_destroy() function, as demonstrated below:

session_start();//Checks if sessions existif(isset($_SESSION<"">) && isset($_SESSION<"">)) //destroys the sessions session_destroy(); //redirects the user to lớn file1.php echo "";?>
Clicking on the logout button will invoke the session_destroy() method. This is shown in the image below:



Sessions are indeed a crucial part of a web application. This article has shown you how khổng lồ create, update, retrieve, & delete sessions. You can, therefore, use this knowledge to lớn craft other powerful applications.