PHP session is not the unknown term for any online users and web developers. While developing an application in PHP, many times developers need to create a login form to authenticate users. By using a login form you can stop unauthorized access to gather the information.
You can differentiate each user and its information with a unique variable called session variable which stores users email id or unique id to identify each user and show its relevant information.
What is PHP Session
A simple HTML page does not allow users to pass the data from one page to another. For this purpose, you can use PHP session which allows you to do this.
A PHP session is a process of accessing users data in each page with a unique session identifier. A unique identifier generates randomly each time when a new session starts. A session variable stores the information in the server rather than in the browsers.
Anyone can access and change the information stored in the browsers. So, the session is the safe way to store and view users information.
Why you should use PHP sessions
Many websites like shopping and membership websites using PHP sessions to store user information for later use. You just need to start the session and call the PHP $_SESSION variable to store session values.
However, when the user left the website, the session variable deleted automatically. This process repeats itself when the user again accesses the website and the random session id is generated each time user makes a login.
How to Start PHP Session
To store any session variable, you need to first start a session by using the functionsession_start()
.
The function first checks if there is already a session exists or not. If the session does not exist, it starts the session with the new session id but if the session already exists, it continues the session with the existing session id.
Syntax
1 2 3 |
<?php session_start(); ?> |
Store and Access PHP session Data
To store a session variable, you need to use the $_SESSION superglobal variable. $_SESSION is an associative array store all the session variables.
You can access the session data when it is present in the session. After the session destroys, the session data disappears from the superglobal array.
Below is a simple example to show how to start a session in PHP and store the session data.
1 2 3 4 5 6 7 8 9 10 11 |
<?php //start a session session_start(); //store a session data $_SESSION["username"] = "Admin"; $_SESSION["mobile"] = "1234567890"; //access the session data echo "Welcome ".$_SESSION["username"]." your mobile number is ".$_SESSION["mobile"]; ?> |
Output
Use PHP Isset Function to Check if PHP Session Variable Exist
You can use PHP isset function to check whether the session variable contains some data or not. PHP isset function is the function used to check if the given variable contains some value or is set or not.
It’s very helpful to check the existence of the session variable and execute a block of code only if the session variable contains some valid value or has some existence.
See below example to check the use of the isset function to check the existence of the session variable.
1 2 3 4 5 6 7 8 9 10 |
<?php //start a session session_start(); if(isset($_SESSION["username"])){ echo "Welcome ".$_SESSION["username"]." your mobile number is ".$_SESSION["mobile"]; else{ echo "Please login to your account first." } ?> |
Output
In above example as the session variables are not set, the code prints the echo statement present in the else condition.
Cleaning sessions variable
Suppose you have a shopping website and you want to clean the session variable and remove its data after the user completes the purchase. You can do so by using the functionsession_unset
.
Below is the example to show you how you can do this.
1 2 3 4 5 6 7 |
<?php session_start(); $_SESSION["username"] = "Admin"; if(isset($_SESSION["username"])){ session_unset($_SESSION["username"]); } ?> |
Destroy PHP sessions
To remove any session variable, you can destroy a session by using the functionsession_destroy()
.
The function destroys the session completely and removes all the session data.
1 2 3 |
<?php session_destroy(); ?> |
You must read:-
- Learn PHP while, do…while and for loops
- PHP Get Post Methods
- How to use Foreach loop to iterate through array
Reference
hi. how can I connect to the database with php7 and mysqli
To connect to MySQL database, you can follow my post, Connect MySQL database using php.