A more accurate counter

  • cipher
  • Graduate
  • Graduate
  • User avatar
  • Posts: 157

Post 3+ Months Ago

I want to create a counter that will not increase by one whenever someone refreshes the page because it is simply not accurate. What I want to do though is to add to the count if the length of the session is 0 (if it is possible with sessions) or if the time spent at the page is 0. if it can work with sessions i don't mind if the count would increase after the browser is restart (when the session has ended) because i think that it is a more appropriate measure than the page increasing everytime it is refreshed. I know that i could do it for unique hits but i don't really want to.

This is what the code looks like:


$server = 'localhost';
$username = 'changethis';
$password = 'changethis';
$database = 'changethis';

$db = mysql_connect("$server", "$username", "$password") or die("Could not connect.");
if(!$db) die("There is no database present.");
if(!mysql_select_db("$database",$db)) die("There was no database selected.");

$res = mysql_query( "SELECT hits FROM hits WHERE (page = '$PHP_SELF')");
$row = mysql_fetch_array($res);
$hit_count = $row[hits];
$hit_count2 = $hit_count + 1;
$sql = "UPDATE `hits` SET `hits` = '$hit_count2' WHERE (page = '$PHP_SELF')";

mysql_query($sql) or die("Unable to process query: " . mysql_error());

$res2 = mysql_query("SELECT hits FROM hits WHERE (page = '$PHP_SELF')");
$row2 = mysql_fetch_array($res2);
$hit_count3 = $row2['hits'];

echo "$hit_count3";

/* CREATE TABLE `hits` (
`hits` int(50) NOT NULL default '0',
`page` varchar(50) NOT NULL default ''

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

I don't understand why you are using the filename of the script in the count. If you want to make a site-wide visit counter, then you may use sessions, or even cookies for that job. I think saving a temporary cookie in the user browser will work ok. Then, if the cookie is present, you don't count a hit. If the cookie isn't defined, you create the cookie and increase the hit counter. Again, that will work for site-wide counter.

If you need counter for every PHP or HTML file in the site, then you need a log analyzer. For Apache logs I use Webalizer, is open source and works great. It computes visits counting sessions of 30 minutes.
  • panreach
  • Novice
  • Novice
  • User avatar
  • Posts: 28

Post 3+ Months Ago

a hit is a hit and a visit is a visit. add stats and don't worry about the problem.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 36 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.