The PHP session helps store user information at the client-side, which can be accessed on multiple pages.
Index
When a user surfs on the internet, the webserver doesn’t know who the user is and what he does. So, session variables are used to identify the user by storing the user’s information to be used on multiple pages.
But, Session variables lose their data automatically after the user has closed the browser. Also, Session variables hold information about the current active user, which is typical for all pages.
In PHP, we can access and modify sessions through the $_SESSION variable.
Read Also: Session Vs Cookies in PHP
The $_SESSION is a global constant in PHP. We can set and access the session data through the $_SESSION variable. In PHP $_SESSION is strictly case-sensitive we can not use $_session or $_Session. Before setting a session, we have to start a session.
session_start() function is used to start session in PHP. This method should be written at the top of the PHP file before any include or require method.
Syntax:
session_start()
Example:
<?php session_start(); // Other PHP Code // Other PHP Code // Other PHP Code ?>
Note: The PHP session’s default time out in the apache config is 30 minutes after the session starts. It can be modified via a php.ini file or HTACCESS file.
In the following steps, we will learn how to start a session and set a session variable in PHP with an example.
Let’s create a page “create_session.php.” Here we generate session variables and put some data into variables.
create_session.php
<?php
session_start(); //start session
$_SESSION["name"] = "NICK"; // Set session variables
$_SESSION["email"] = "test@tempmail.com";
?> Next, we create another page, “getdata_session.php.”
Here we retrieve values of session variables which we set on our page “create_session.php.”
Here we will use the global variable $_SESSION to retrieve data of all session variables. Another essential point is that we do not pass the values of session variables on each page. Instead of that, we retrieve session variables after starting the session using the session_start() function.
getdata_session.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
//checks if session variables are set or not
if(isset($_SESSION["name"]) && isset($_SESSION["email"])){
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["name"] ".<br>";
echo "Email is " . $_SESSION["email"] ".";
}
?>
</body>
</html> Output:
Name is NICK
Email is test@tempmail.com We can modify the session variables by just overwriting its original values.
Let’s create the page “modify_session.php” to alter the session variables.
modify_session.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["name"] = "TEST";
echo $_SESSION["name"];
?>
</body>
</html> Output:
TEST In PHP, session_destroy() function destroys all sessions entirely. But session_unset() removes all session variables. However, both have a similar effect.
Example:
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?> Also Read: MySQLi Functions in PHP
I hope now you have a complete understanding of ‘What is session in PHP with example.’ Even more, PHP sessions are quite easy to implement.
Happy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…