Comprehensive Guide To PHP Sessions

PHP Sessions is a mechanism used to store and share data between multiple pages of a website. It allows you to maintain stateful information across multiple requests from a browser.

When a visitor navigates through different pages on a website, each one contains its own set of data.

For example, login credentials or the contents of a shopping cart. Sessions allow web applications to store and retrieve that data.

When a PHP session is started, data is stored on the server and assigned a unique session identifier (SID) so that later requests from the same user can identify the session data.

Data can be stored in sessions for both the short-term and the long-term, and these sessions can be set up to expire automatically at a particular time or when the user logs out or when the session is closed.

Throughout the article, we will play with the basics of working with PHP sessions so that we will learn how to create and destroy sessions, how to store and retrieve session data, as well as how to configure session settings for optimal performance and security.



What Are PHP Sessions?

You work with an application by opening it, making some changes, and then closing it once you are done. Essentially, this will be the same as a session. Computers recognize you.

It knows when you are starting and when you are terminating the application when you are using it. However, there is a problem with the internet, and it is this: because HTTP addresses are not maintained, the web servers do not know who you are nor what you do.

The solution to this problem is by storing user information (e.g. username, favorite color, etc.) in an element called a session variable so that it can be used across multiple pages. As a default, session variables last until the browser is closed by the user.

As a result, session variables are used to store information about a single user, and they can be accessed by all of the pages in the same application.

Remember: Databases are good for storing data permanently.

PHP Session_start()

Using the session_start() function, you can start a session.

$_SESSION is the PHP global variable that sets the session variables for the current PHP session.

The next thing we need to do is create a new page called sessions1.php. We will be starting our first PHP session on this page, as well as setting some session variables as follows:

Example: 

<?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["fname"] = "Bill"; $_SESSION["lname"] = "Gates"; echo "Variables are set"; ?> </body> </html>
Remember: You should make sure that the session_start() function is the first thing in your document. If it is not, your document will fail. It should be placed before any HTML tags have been added.

PHP Session Variable Values

The next step is to create another page with the name session2.php. Using this page, we will be able to access the information about the session that we had set on the first page (session1.php).

Each new page retrieves session variables from the session we opened at the beginning (session_start()) instead of passing them individually.

There are also a number of session variables that are stored in the global $_SESSION variable, which looks like this:

Example: 

<?php session_start(); ?> <!DOCTYPE html><html> <body> <?php // Echo session variables that were set on previous page echo "First name is " . $_SESSION["fname"] . "<br>"; echo "Last name is " . $_SESSION["lname"] . ""; ?> </body> </html>
Alternatively, you can run the following code in order to show all the session variable values for a particular user session:

Example: 

<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php print_r($_SESSION); ?> </body> </html>

What does it do? How does it know it’s me?

On most computers, the user will be assigned a user-key that will look something like this on their computer: 756487cf34ert8dede5a562e4f3a7e12. Then, when a session is open on a different page, it scans the computer for a user-key when opening that session.

A session will be accessed if there is a match, if there is not a match, then a new session will be started.


Modify PHP Session Variable

A session variable can be changed by simply overwriting it with the new value:

Example: 

<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // to change a session variable, just overwrite it $_SESSION["fname"] = "Melinda"; print_r($_SESSION); ?> </body> </html>

PHP Session_destroy()

When you want to remove all global session variables from a session and destroy it, you can use session_unset() and session_destroy():

Example: 

<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // remove all session variables session_unset(); // destroy the session session_destroy(); echo "Session remove and destroyed" ?> </body> </html>
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *