A Session is used to store information about user throghout the website. Session variables hold information about one single user, and are available to all pages in one application.
Starting a PHP Session:
Before you can store user information in your PHP session, you must first start up the session.
<?php session_start(); ?>
Note: The session_start() function must appear BEFORE the <html> tag
Storing a Session Variable:
The correct way to store and retrieve session variables is to use the PHP $_SESSION variable
<?php session_start(); // store session data $_SESSION['views']=1; ?>
Retrieve session data:
<?php echo "Pageviews=". $_SESSION['views']; ?>
Destroying a Session:
With use of unset() and session_destroy() functions we can delete session datas.
The unset() function used to remove value from single variable. For example,
<?php session_start(); if(isset($_SESSION['views'])) unset($_SESSION['views']); ?>
You can also completely destroy the session by calling the session_destroy() function:
<?php session_start(); session_destroy(); ?>