Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!I need to make a hit counter.
I used this code in the counter.php file:
PHP
<?PHP
  session_start();
 
  if(isset($_SESSION['views'])){
     $_SESSION['views'] = $_SESSION['views']+ 1;
  }else{
     $_SESSION['views'] = 1;
  }
  echo "Total page views = ". $_SESSION['views'];
?>


and used this code in the file where i will be using my hit counter:
<?php
  echo "<hr><div align=\"center\">";
 
  include_once "counter.php"; // this will include the counter.
 
  echo "</div>";
?>


but I don't want the counter to increase on refreshing the page. This is not my code.I copied it from some site. Any help would be appreciated.
Posted
Comments
Graham Breach 22-Nov-12 7:31am    
I think you have the wrong code for a hit counter. Storing the number of views in the session will tell the user how many page views they personally have had (and only in the current session) and not how many views in total that the site has had.

You need a counter that stores the count in a database or file on the server.
Smrng 22-Nov-12 23:49pm    
do you have any code for the same?
Killzone DeathMan 23-Nov-12 4:47am    
Its better using DataBase, if you close the browser and open again, the conter will be reset, because the php session is reset to!
Better is to store into you DataBase each time you refresh the page!
Smrng 26-Nov-12 1:59am    
ok thnks!!

Hello Smrng,

You can also do it with the help of cookies and Database.
 
Share this answer
 
Hi Smrng

Check this out:

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')

Regards
Pawan

Note: Please accept the solution if it fulfills your critirea
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900