You must have heard the word session before, If not then basically in PHP it holds the data for a limited time period. In this article, you will know what is significant of it and how to use its different functions. So let’s see everything you should know about sessions in PHP.
Sessions hold some data and persists all over the website for a limited period of time.
So assume you are creating a website that has login and logout feature and you want to show some features of your website only to logged-in users, not to others. So to make this scenario complete you will need the Session feature of PHP. Whenever a new user login you will create a session for that user so that he can see all features.
How to create sessions
The above scenario sounds cool, now let’s see how we can do that practically. Suppose you have a form and whenever someone submits, it will check that data with the database. If the data is matched, the login will be successful. So once the login is successful then we can create a session.
Before you create sessions in PHP you have to use the
session_start() function of PHP. This function should be on the first line of the file.
To create a session we use $_SESSION, which is a superglobal variable of PHP. Let’s see how you can use it to create a session and store some data in it.
session_start(); //on top . . . $_SESSION["loggedin"] = true; //could be anywhere in same file
So as you can see $_SESSION is an array with key “loggedin” and value “true”.
You would wonder it’s like a normal array, yes that’s true, but it is predefined and superglobal variable. So what is superglobal variable here ?, Basically its called superglobal variable because you can access this key “loggedin” and value “true” from any page of your project.
Access sessions on any page
So now we have created a session, let’s see how we can access this session on any other pages in our project/website.
Important – so either you are creating, accessing, or destroying a session in PHP, the topmost line always will be session_start() in the file.
To access the value of session we created above , we will just use just key.
session_start(); //on top . . . $_SESSION["loggedin"]; //could be anywhere in same file $isLoggedin = $_SESSION["loggedin"]; //put session value in variable echo $_SESSION["loggedin"]; //view value stored in this session
Destroying session in PHP
Although you are about to destroy a created session, you will still need session_start() function at the top.
To destroy sessions in PHP you can simply use
session_start(); //on top session_destroy();
Warning : As you could create multiple sessions with different key and values but when you use
session_destroy() function, then it destroys all sessions you have created.
Sometimes you need to destroy only single or few sessions not all. So, in that case, you can use
unset() function of PHP. But do not forget session_start() at top.
session_start(); //on top unset($_SESSION["loggedin"]); //only remove loggedin session from all sessions array.
So that was all about everything you should know about sessions in PHP. If you have any queries or something let me know in the comment box below.