PHP $_SESSION

Sessions in PHP are started by using the session_start() function. Like the setcookie( ) function, the session_start( ) function must come before any HTML, including blank lines, on the page.

Example:

index.php:

<?php session_start();
$numero = rand(100, 999);
$_SESSION['rand'] = $numero;
echo $_SESSION['rand'];
?>
<br><a href="session.php">session.php</a>

session.php:

<?php session_start();
echo $_SESSION['rand'];
?>
<br><a href="destroy.php">destroy.php</a>

destroy.php:

<?php session_start();
echo $_SESSION['rand'];
session_destroy();
?>
<br><a href="index.php">index.php</a>

Leave a Reply