Hindi Songs

Search About PHP!!!

Wednesday, July 1, 2009

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;
? >

< html >
< body >

< ?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
? >

< /body >
< /html >
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:
< ?php
session_start();

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
? >