php session() code example
Example 1: session storage
sessionStorage.setItem('key', 'value');
let data = sessionStorage.getItem('key');
sessionStorage.removeItem('key');
sessionStorage.clear();
Example 2: php sessions
<?php
session_start();
$_SESSION['item_name'] = 'value';
$_SESSION['item_name'] = 0;
$_SESSION['item_name'] = 0.0;
$value = $_SESSION['item_name'];
?>
Example 3: php session variables
<?php
session_start();
$_SESSION['var'];
?>
Example 4: php session
/*Sessions are stored on the server
Sessions are a way to carry data across multiple pages.
Typically if we set a variable on one page, it wouldn't be available
on the next page. This is where Sessions come in. Unlike cookies session
data is not stored on the user's computer. It is stored on the server.
In order to use session variables you have to start a session.
Every page, that you want to use that data in, you have to use
session_start.
If you want to unset one of these sessions you can use session_unset
youcan destry the session with session_destroy
*/
<?php if(isset($_POST['submit']))
{ session_start();
$_SESSION['name'] = htmlentities($_POST['name']);
$_SESSION['email'] = htmlentities($_POST['email']);
header('Location: page2.php'); } ?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Sessions</title>
</head>
<body>
<form method="POST" action="<?php echo $server['PHP_SELF'];?>">
<input type="text" name="name" placeholder="Enter Name">
<br>
<input type="text" name="email" placeholder="Enter Email">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Example 5: session management
Session is a State Management Technique.
A Session can store the value on the Server.
It can support any type of object to be
stored along with our own custom objects
Example 6: session
<?php
session_start();
$_SESSION['prenom'] = 'Jean';
$_SESSION['nom'] = 'Dupont';
$_SESSION['age'] = 24;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Titre de ma page</title>
</head>
<body>
<p>
Salut <?php echo $_SESSION['prenom']; ?> !<br />
Tu es à l'accueil de mon site (index.php). Tu veux aller sur une autre page ?
</p>
<p>
<a href="mapage.php">Lien vers mapage.php</a><br />
<a href="monscript.php">Lien vers monscript.php</a><br />
<a href="informations.php">Lien vers informations.php</a>
</p>
</body>
</html>