Quick Links
Using a PHP Session, you can store information in variables, to be used across multiple pages.By default, session variables last until the user closes the browser.
PHP session_start() function
A session is started using the session_start() function. Use the P.H.P global $_SESSION to set session variables.
Syntax :
bool session_start ( void )
Example :
session_start();
PHP $_SESSION
P.H.P $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.
Example: Store information
$_SESSION["user"] = "Sachin";
Example: Get information
echo $_SESSION["user"];
Session Example
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION["user"] = "Sachin";
echo "Session information are set successfully.<br/>";
?>
<a href=".....">Visit next page</a>
</body>
</html>
Session Counter Example
<?php
session_start();
if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1;
} else {
$_SESSION['counter']++;
}
echo ("Page Views: ".$_SESSION['counter']);
?>
Destroying Session
P.H.P session_destroy() function is used to destroy all session variables completely.
<?php
session_start();
session_destroy();
?>