Storing a Session Variable
The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:
session_start();
// store session data
$_SESSION['views']=1;
?>
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
Output:
Pageviews=1
In the example below, we create a simple page-views counter. The isset() function checks if the "views" variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't exist, we create a "views" variable, and set it to 1:
session_start(); if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
|