session_start()
function. This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start()
at the beginning of the page._SESSION[]
. These variables can be accessed during lifetime of a session.<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Sessions</title>
</head>
<body>
<?php
$_SESSION["user"] = "FTL";
echo "Session are set successfully..!<br/>";
?>
<h2><a href="http://www.freetimelearning.com/php/php-sessions.php" target="_blank">PHP Sessions</a> </h2>
</body>
</html>
The PHP Session is mainly use of isset()
function to check if session variable is already set or not set.
The following example are php session counter :
<?php
session_start();
if( isset( $_SESSION['count'] ) ) {
$_SESSION['count'] += 1;
}else {
$_SESSION['count'] = 1;
}
$message = "This page total visitors are : ". $_SESSION['count'];
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Session Counter</title>
</head>
<body>
<?php echo ( $message ); ?>
</body>
</html>
PHP session_destroy()
function is used to destroy all session variables completely. This function is mainly used logout
(or) signout
pages.
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Session Destroy</title>
</head>
<body>
<?php
session_destroy();
?>
</body>
</html>