Courses Job Ready Fresher Training AI for Class 7 to 12 Corporate Training Placements Tutorials
Free Learning Resources

IT Tutorials & Interview Prep

Free guides, interview Q&As, and job responsibility breakdowns — curated by industry veterans to help you crack MNC interviews

109+
Tutorial Articles
9
Topic Categories
100%
Free to Read
← Back to PHP

$_SESSION

PHP Last Updated: Oct 17, 2025

$_SESSION

A session is a way to store information to be used across multiple pages. Session variables store user information which can be used across multiple pages. Session variables last until the user closes the browser.

Starting a PHP Session

In PHP, a session can be started with the session_start() function. Session variables are set with the $_SESSION variable. Let's assume we have one webpage called example1.php. On this page, we will start a new PHP session and set session variables.

example1.php

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["uname"] = "shayan";
$_SESSION["upass"] = "hunter";
echo "Session variables are set.";
?>

</body>
</html>

Get PHP Session Variable Values

Now we will create a new page named example2.php. From this page, we will access the session variables we have set on the first page (example1.php).

example2.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["uname"] . ".<br>";
echo "Favorite animal is " . $_SESSION["upass"] . ".";
?>

</body>
</html>

We can also see all the session variables by using print_r($_SESSION).

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>

Modifying a Session Variable

To modify a session variable, we just need to overwrite it.

Example:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// to change a session variable, just overwrite it
$_SESSION["uname"] = "aakash";
print_r($_SESSION);
?>

</body>
</html>

Destroying a PHP Session

To delete all the session variables and destroy the session, we can use session_unset() and session_destroy().

Example:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset();

// destroy the session
session_destroy();
?>

</body>
</html>

Want to practice these scenarios in a real IT environment? Our 45-Day Internship Program gives you hands-on experience with Active Directory, Ticketing Tools, and Networking.
Join the Internship →