PHP Hit Counter

A small but not that simple hit counter.

<?
// start at the top of the page since we start a session
session_name('mysite_hit_counter');
session_start();
//
$fn = 'hits_counter.txt';
$hits = 0;
// read current hits
if (($hits = file_get_contents($fn)) === false)
{
	$hits = 0;
}
// write one more hit
if (!isset($_SESSION['page_visited_already']))
{
	if (($fp = @fopen($fn, 'w')) !== false)
	{
		if (flock($fp, LOCK_EX))
		{
			$hits++;
			fwrite($fp, $hits, strlen($hits));
			flock($fp, LOCK_UN);
			$_SESSION['page_visited_already'] = 1;
		}
		fclose($fp);
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Hit Counter Example Code</title>
</head>
<body>
<p>Page content...</p>
<div class="counter">
	<p>This page has <span><?=$hits;?></span> hits</p>
</div>
</body>
</html>

Usage

What would a PHP code site be without a hit counter? So I am throwing in my version of this script. Using file_get_contents() for fast reading of the hits, implements a session as not to record a hit for the same user again and file locking for safe and multiple writes. Of course use as is since flock() can block the script execution if you have many visitors! For non Windows users you can use a non-blocking flock(): <pre> if (flock($fp, LOCK_EX | LOCK_NB)) </pre> and run a timed loop to wait for some milliseconds before aborting the locking. Obviously that would be an overkill of a tiny hit counter.


Comments

guest
Posted on 02.08.2018 09:54

Its works perfectly.Thanks for the code.

guest
Posted on 16.04.2014 20:59

thanks...
finally, it helps me out.. :)

guest
Posted on 13.04.2013 11:12

thank u...

guest
Posted on 24.02.2013 22:40

Thanks! This is the best way to do it with a simple file as database and
You can also include it anywhere you whant, all compatible!
Ipressive! Love it! :)

guest
Posted on 28.12.2012 09:16

thank you very much.....

guest
Posted on 22.09.2012 03:04

thanks!

guest
Posted on 05.09.2012 14:35

where I keep this php file and how to use this php file or may i use only code in my index html file? jst wanna know.am only a beginner. thanks a lot...
websofteen@gmail.com

guest
Posted on 25.05.2012 08:04

Thank You.... The most best counter script ever... Thanks again....

guest
Posted on 10.05.2012 11:11

thank you

guest
Posted on 13.02.2012 08:23

Thanks for your information. This is helpful for my work. Again thanks. http://www.mdrayhankhan.com by mdrayhankhan

guest
Posted on 12.02.2012 13:33

nice...........

vsrinivasarao
Posted on 21.09.2011 04:58

hi

guest
Posted on 18.08.2011 16:05

Neat but its not accurate. If two scripts read and the same time and bump the hits by one, each will right a hits+1 and one count will be lost. You have to read inside the flock.

guest
Posted on 02.02.2011 09:33

this is perfect

guest
Posted on 14.01.2011 23:06

great! Thanks for this script! :D
I used it on my webpage for downloads count.
http://javildesign.com

guest
Posted on 11.11.2010 20:49

Thanks for the code !! =) works perfectly !

Add your comment