In this tutorial, we want to discuss PHP Session and how to use it. Which we can learn with the help of examples.
What Is A Session In PHP?
A PHP Session is a way to keep information from one web page to the next so that users can be recognized as they move around a site or app.
Do you want to know why a website needs sessions?
To understand why sessions are important, we need to look at how the HTTP protocol is set up.
The HTTP protocol is stateless, which means that a server can’t remember a specific user between requests. For example, when you visit a website, the server’s only job is to give you the information you asked for.
So, when you go to other pages on the same website, the web server handles each request as if it had nothing to do with the others. The server has no way of knowing that each request comes from the same user.
How Do You Start A PHP Session?
A session is started with the
Session variables are set with the PHP global variable
And now, let’s create a program with the file name “test_session1.php” to start a new session and set some session variables:
<?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["name"] = "Angel Jude Suarez"; $_SESSION["position"] = "Programmer"; echo "Session variables are set."; ?> </body> </html>
session_start() must be placed in the first part of the code of your php file before any HTML tags.
GET The Values Of PHP Session Variables
Next, we will create another page with the file name “test_session2.php“. From this page, we will GET the values of session variables we set on the first page with the file name “test_session1.php“.
Notice that the session variables in PHP are not passed individually to each new page, instead, they are retrieved from the session we open at the beginning of each page.
Here’s the program for getting the values of session variables set on the first page name “test_session1.php“.
<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Echo session variables that were set on previous page echo "Name is: " . $_SESSION["name"] . ".<br>"; echo "Position is: " . $_SESSION["position"] . "."; ?> </body> </html>
When you execute the program this will be the output:
Name is: Angel Jude Suarez
Position is: Programmer
Destroy A PHP Session
To remove all global session variables and destroy the session, we will use the
Here’s the program in destroying global session variables:
<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // remove all session variables session_unset(); // destroy the session session_destroy(); ?> </body> </html>
Frequently Asked Questions
PHP responds by sending a unique token that identifies the current session. This is known as the session ID. In all subsequent requests, the browser sends the session ID to say, “Hello IT Source Coders.” All other data related to the session is stored on the web server. Only the session ID gets passed back and forth.
A session started with the
Before you can store any information in session variables. The
must be placed in the first part of the code of your php file before any HTML tag.
- PHP Chmod Dir Function With Examples
- Session Name PHP With Code Example
- PHP Read File Line By Line With Example
- Session Timer PHP With Detailed Explanation
- PHP Remove Element From Array With Examples
We have completely discussed the Session functions from
session_destroy(). In this tutorial, we have learned the function of Session with the help of examples. I hope this simple PHP Tutorial will help you a lot.
By the way, If you have any questions or suggestions about this article entitled “Session in PHP“. Please feel free to comment below.