Start a Session in PHP
Create demo.php file.
Create session in demo file by starting with the session_start()
function.
Session variables are set with the PHP global variable: $_SESSION.
Implement code in demo.php
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Session in PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="text-center">
<h1>Start session</h1>
</div>
<div class="well">
<?php
// Set session variables
$_SESSION["Username"] = "Kiran";
$_SESSION["UserId"] = "123456789";
echo "Session variables are set.";
?>
</div>
<br>
</div>
</body>
</html>
Get PHP Session variable values from one page to another.
Create demo2.php file.
Create session in demo2 file by starting with the session_start()
function.
session variables are not created individually in new page, instead they are retrieved from the session we open at the demo.php file beginning session_start()
. from that variable passed to demo2.php
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Session in PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<br>
<div class="text-center">
<h1>Get PHP Session variable values from one page to another</h1>
</div>
<br>
<br>
<br>
<div class="well">
<?php
// Echo session variables that were set on previous page
echo "<h1>User name is " . $_SESSION["Username"] . ".<br></h1>";
echo "<h1>User Id is " . $_SESSION["UserId"] . ".</h1>";
?>
</div>
<br>
</div>
</body>
</html>
We can also Modify a PHP Session Variable
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Session in PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<br>
<div class="text-center">
<h1>Start a Session in PHP</h1>
</div>
<br>
<br>
<h2></h2>
<div class="well">
<?php
// Set session variables
$_SESSION["Username"] = "Shiva";
$_SESSION["UserId"] = "123456789";
print_r($_SESSION);
echo "Session variables are set.";
?>
</div>
<br>
</div>
</body>
</html>
Delete or Destroy created session id in a PHP Session
use session_unset()
and session_destroy()
u to remove all session variable
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Session in PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="text-center">
<h1>Delete or Destroy created session id in a PHP Session</h1>
</div>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
<br>
</div>
</body>
</html>