Storing a Session Variable
The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:
Output:
page views 1
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> |
page views 1
<?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?> |
output:
In the example , 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:
0 comments:
Post a Comment