PHP HOME
PHP Introduction
PHP SETUP
PHP Syntax
PHP Data Types
PHP Outputs
PHP Variable
PHP Constant
PHP Strings
PHP Operators
PHP if else if
PHP Switch
PHP while loop
PHP for loop
PHP Arrays
PHP Functions
PHP Sorting Array
PHP Form Handling
PHP Form Validation
PHP Form Required
PHP Form URL/EMAIL
PHP Form Submit
PHP Contact Form
PHP DATE & TIME
PHP INCLUDE
PHP FILE HANDLING
PHP FILE OPEN/READ
PHP FILE CREATE/WRITE
PHP FILE UPLOAD
PHP SESSION
PHP COOKIES
PHP FILTER
PHP ERROR HANDLING
PHP EXCEPTION
PHP Redirects
PHP with MySQL
PHP WITH MYSQL DB
MYSQL CONNECT
MYSQL CREATE DATABASE
SELECT DATABASE
MYSQL CREATE TABLE
MYSQL INSERT DATA
MYSQL SELECT DATA
MYSQL UPDATE DATA
MYSQL DELETE DATA
MYSQL ORDER
MYSQL LIMIT
MYSQL LAST ID
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.
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