Jump to content

Unique Page views


siri

Recommended Posts

Friends,How to display the no of active users online and how to create a unique page views.I tried the following code:*********************************************************************<?phpsession_start();if(isset($_SESSION['views'])){$_SESSION['views']=$_SESSION['views']+1; if(isset($_COOKIE['visitors'])) { $_SESSION['views']=$_SESSION['VIEWS']-1; }}else{$_SESSION['views']=1;setcookie("visitors",1);}echo "Views: ".$_SESSION['views'];?>*************************************************************************Initially when I run this program it shows 1 but when I reload the page it displays as -1 .So how to create the unique page views.I tried to use cookies so that if the same visitor reloads the page then the session variable must be the same.So where exactly is the mistake.

Link to comment
Share on other sites

Do you want it to just display how many times the user has visited the page while the browser has been opened? If you want to have it display how many unique views the page has gotten you will need to have a database and in the database have the time visited and the persons ip address. Just google it and you will find some downloadable packages :).Here is your code and it should be fixed:

<?phpsession_start();$_SESSION['views']++;if(isset($_COOKIE['visitors'])){$_SESSION['views']--;}else{setcookie("visitors",1, time()+(60*60*24*365));//sets cookie visitor with a value of "1" for a year}echo "Views: ".$_SESSION['views'];?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...