saftutorials.com
PHP SESSION Tutorial

The $_SESSION variable is used when we want to access the variable at multiple pages. This is commonly used for login and logout functionality in the panel. A session variable creates a temporary file on the server where its name and values are stored, and this session data will be available on all pages of an application.

When do you want to use session variables in your project?
so first you have to start the session after that assign the value into the session and lastly, use that session variable value at same page or any other pages of the same project.

This function is used for session_start() in PHP.


Below is the example of use of session variables

Example of PHP session start and assign some value


Create a test_session1.php page

<?php
// Start the session
session_start();
// Set session variables
$_SESSION["email"] = "john@gmail.com";
$_SESSION["mobile"] = "+95 9897655467";
$_SESSION["name"] = "John";
?>
<!DOCTYPE html>
<html>
<body>
<h1>Start session variable, set values and echo that values</h1>
<?php
echo "Email ID : ".$_SESSION["email"]."<br />";
echo "Mobile No : ".$_SESSION["mobile"]."<br />";
echo "Name : ".$_SESSION["name"];
?>
</body>
</html>

The above code produces the following results

Outputs: Email ID: john@gmail.com

Mobile No: +95 9897655467

Mobile No: John



create a test_session2.php page

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<h1>Start session variable, set values and echo that values</h1>
<?php
echo "Email ID : ".$_SESSION["email"]."<br />";
echo "Mobile No : ".$_SESSION["mobile"]."<br />";
echo "Name : ".$_SESSION["name"];
// You can print all session values by using print_r() function
print_r($_SESSION);
?>
</body>
</html>

The above code produces the following results

Outputs: Email ID: john@gmail.com

Mobile No: +95 9897655467

Mobile No: John


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic