hi
i want to calculate the no of user visited our site.can somebody please suggest how to do that
thanks in advance

you just create a table with column like view..
the increment that column every time user visited your site like:

if(isset($_GET['view']))
{
 $r="update viewtable  set views=(views+1)  ";
$r1=mysql_query($r);
}
 header("location:yournextpage.php");

and use this:

<a href="www.yousite.com?view" >

You could also use Google Analytics. It will give you a hit count, average time on site, keywords used to find the site, traffic sources (referring site), breakdown of stats of which search engine was used to find your site, pages per visit, a global map view of where the hits came from, individual page stats, and more!

I agree with Buddylee17, Google Analytics is the only way to go if you want detailed information on who visits your site.

Or if you purely want something that counts visitors, you could use something simple like this.

<?php
 $counter_file = ("counter.txt");
 $visits = file($counter_file);
 $visits[0]++;
 $fp = fopen($counter_file , "w");
 fputs($fp , "$visits[0]");
 fclose($fp);
 echo "Hits: " . $visits[0];
?>

Then just make a blank text file named "counter.txt" in the same directory as the file that contains this snippet. You could even go as far as to create a cookie to make sure you don't get duplicate entries from the same visitor. I know this isn't great, but it's just a thought.

hi i tried shanthi's counting....it works fine but each time when i refresh the count is getting incremented.....i want a browser to be counted as 1 ie when user closes the browser then it can counted but counting every time when refresh button is clicked is not wat i desired...plaese tell me how to do tat

I've taken code from Shanthi's post and edited to to set a cookie when a user visits the page. When the user refreshes the page, it checks for the cookie. If the cookie exists, it does not add to the counter, and if it doesn't, it does add to the counter. Afterward, it prints the hits.

<?php
$filename = 'hitcount.txt';
$handle = fopen($filename, 'r');

if (!$_COOKIE['counter'] && $handle) {
 setcookie("counter", 1, time()+3600);
 $hits = trim(fgets($handle)) + 1;
} else {
 $hits = trim(fgets($handle));
}

fclose($handle);
echo $hits;
?>

If you're using a different one, let me know and I can make similar changes to it.

Comments
nice

Actually, I tried my code and it didn't work. :P

Here's one that works like I described.

<?php
$filename = 'hitcount.txt';

if (!$_COOKIE['counter']) {
 setcookie("counter", 1, time()+3600);
 $handle = fopen($filename, 'r');
 $hits = trim(fgets($handle)) + 1;
 $handle = fopen($filename, 'w');
 fwrite($handle, $hits);
} else {
 $handle = fopen($filename, 'r');
 $hits = trim(fgets($handle));
}

fclose($handle);
echo $hits;
?>
This article has been dead for over six months. Start a new discussion instead.