In this tutorial, we will be learning how to use PHP sessions.
Pay extra attention to this lesson: this is one of PHP's most useful feature.
What Is A Session?
A session is another way that you can temporarily store data to be used in multiple pages of your website.
As long as you have not cancelled or destroyed the session, the data will remain saved; You can leave the website and come back, and that same data will still accessible on that same page.
Why Use Sessions? We use session because with sessions, it is possible to write code so that when a user closes a browser without logging out, their information remains saved on that page.
The most common and obvious application of sessions is for creating login pages: if the user has not logged in, they see one page(e.g. a please sign in screen). After the user has logged in, they see another page (e.g. their own account information).
In that case, we use sessions to keep track of whether the user has logged in or not.
Session Or Cookies?
Similar to cookies, session is a way of storing data. However, unlike cookies, this data is not stored on the user's computer.
Usually, we use sessions for temporary information that we don't want to expire after a certain amount of time has passed.
Session will remain in the browser even if you close a browser.
Sessions are more secure than cookies since their data is stored in the server-side.
It's not easy to select or update session data, so often we store more sensitive data in sessions rather than cookies.
Starting a Session
To start accessing your session data, you need to run the session_start()
function. Executing this function will allow you to access your session variables.
Session variables are set using a global variable called $_SESSION
(as a reminder, global variables are variables that are accessible on every page of the website).
After you've started your session_start(), you are free to set up/create session variables.
For example, the syntax we use to create new session variables is as follows: Syntax
<?php
$_SESSION["name"] =value;
Name = session variable name.
Value = session variable value.
?>
Session PHP Sessions Variable Values
The whole concept of session variables will be much easier to understand when you see an actual example in action. So let's create a few pages of related code.
To begin, start by creating a file named "session_demo_one.php".
Example
<?php
// Start our session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Below we set our session variables
$_SESSION["favcolor"] = "red";
$_SESSION["favanimal"] = "rabbit";
echo "We have set two session variables and their values are red and rabbit";
?>
</body>
</html>
Result
Note: make sure session_start() is above all your HTML tags.
Ensure not to echo any $_SESSION
variable before you run the session_start()
function, or it will produce an error.
Getting PHP Session Variable Values
Next, let's create another file called "session_demo_two.php". Add the following code in that file.
In this page, we will access the session variables we set in session_demo_one.php.
Add the following code to "session_demo_two.php".
<?php
// Start our session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Below we set our session variables
echo "The value of $_SESSION['favcolor'] is:".$_SESSION["favcolor"];
echo '<br>';
echo "The value of $_SESSION['favanimal'] is:".$_SESSION["favanimal"];
?>
</body>
</html>
Result
As you can see, session variables can be used in different pages of the same website.
The sessions value do not disappear when you move to a different page.
By simply by running session_start() you can access the same session variables you set in an earlier page.
Note that we are accessing our session variables using the global variable $_SESSION
.
You can also access all of your session variables using the print_r()
function.
Example
<?php
// Start our session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION)
?>
</body>
</html>
Modifying A Session Variable You can override a session variable in the exact same way you override an ordinary variable. Below, create a page called session_demo_three.php and add the following code:
Example
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION['favcolor']='black'
?>
</body>
</html>
If you return to session_demo_two.php, $_SESSION['favcolor'] will have a value of black.
Ending A Session
If you no longer wish to store the information, you should end the session. In order to end a session, you must delete all the session variables.
This is usually because the user wants to log out.
To erase all the session data, you can use two different methods.
The methods to use is session_destroy()
.
Example
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// destroys the session
session_destroy();
?>
</body>
</html>
However, as an additional safety precaution, you can first set all the sessions variables to NULL before you use session_destroy()
.