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.
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 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: 
Example: 
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_destroy()
When you want to remove all global session variables from a session and destroy it, you can use session_unset() and session_destroy():