PHP sessions allow website applications to store user information on the server. As a result, the data can be accessed from several pages throughout a website.
Bạn đang xem: How to use sessions and session variables in php
Sessions rely on an identifier khổng lồ 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 unique session identifier or ID is used lớn identify a specific user. Therefore, when a session ID is null, it implies no session has been created yet. Hence, PHP is prompted khổng lồ initiate one.
In this tutorial, we will learn how to start sessions và 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 to start a session
We need lớn store user data in session variables before they can be accessed across multiple web pages. Therefore, our first step is lớn 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 chất lượng 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 lớn 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 và then include the following code:
You should have the following output:
Successfully phối the session variables.
Accessing a created sessionNow that we already have our sessions in the preceding darkedeneurope.com, let’s retrieve them to lớn ensure that they were set successfully.
Create an accessdetails.php file. We will use this tệp tin to access the previously set session variables.
We will need to lớn use a conditional statement to lớn 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 know you enjoy writing too!
In case you misspelled the session variables, PHP may not recognize the data you are referring to lớn and, therefore, return the else statement.
Sorry.. No such session variables set!
Ensure you place a semicolon at the kết thúc of a PHP statement lớn avoid syntax errors that prevent your code from running.
How lớn update a mix session
In some cases, we may want khổng lồ change a session variable to lớn 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 to Liz Muganga!
Destroying a session.The whole point of using sessions was khổng lồ store user’s data and make it accessible throughout the website application.
Therefore, the hệ thống can determine who is accessing the application using the quality 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 phối session variables. It does not require any parameters.
The following code shows how to 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 darkedeneurope.com.
We will create a simple website application that uses PHP sessions. We will utilize Bootstrap styling lớn save time.
The first step is lớn create a file and name it file1.php. In this file, địa chỉ the following code:
//Starting the Sessionsession_start();//Accessing session variables$_SESSION<'name'> = 'John Doe';$_SESSION<'email'> = 'john
gmail.com';?>html lang="https://www.darkedeneurope.com/en"https://www.darkedeneurope.com/>head> meta charset="https://www.darkedeneurope.com/UTF-8"https://www.darkedeneurope.com/> meta http-equiv="https://www.darkedeneurope.com/X-UA-Compatible"https://www.darkedeneurope.com/ content="https://www.darkedeneurope.com/IE=edge"https://www.darkedeneurope.com/> meta name="https://www.darkedeneurope.com/viewport"https://www.darkedeneurope.com/ content="https://www.darkedeneurope.com/width=device-width, initial-scale=1.0"https://www.darkedeneurope.com/> title>PHP Projecttitle> link rel="https://www.darkedeneurope.com/stylesheet"https://www.darkedeneurope.com/ href="https://www.darkedeneurope.com/https://fonts.googleapis.com/css?family=Roboto:ital"https://www.darkedeneurope.com/> link href="https://www.darkedeneurope.com/https://cdn.jsdelivr.net/npm/bootstrap
When you navigate to lớn your browser, the web page should look, as shown below:
Neema Muganga is a Tech enthusiast who enjoys creating web applications using React và Django tools. She is particularly interested in networking with peers, exploring & learning new tech skills. Neema sparks so much joy in kinh doanh and spending time alone.