Table of Contents
A session is a way to store information (in variables) to be used across multiple pages. By default, session variables last until the user closes the browser.
<?php
/**
* Start_PHP_Session.php
*/
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>phpTeeratus</title>
</head>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
Result View Example
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<h4>Echo_session_variables</h4>
<?php
// Echo session variables that were set on previous page
echo "ex2 Echo_session_variables";
echo "Favorite color is " . $_SESSION['favcolor'] . ".<br>";
echo "Favorite animal is " . $_SESSION['favanimal'] . ".<br>";
echo "ex2" . ".<br>";
print_r($_SESSION);
?>
</body>
</html>
Result View Example
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<body>
<h2>phpTeeratus</h2>
<p>From w3schools.com, Experiment by Teeratus_R </p>
<h4>Destroy a PHP Session</h4>
<p>
To remove all global session variables and destroy the session, use session_unset() and session_destroy():
</p>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
print_r($_SESSION);
echo "<br>";
echo "All session variables are now removed, and the session is destroyed.";
?>
</body>
</html>
Result View Example
Document in project
You can Download PDF file.