html visitor counter source code using PHP

A tiny article but not that simple html visitor counter source code.

Hit counter in PHP

hello guys i am learning to main 6 step to create a html visitor counter source code step by step.

Step 1: Set the variable to file total_save_domain_name_hits_counter.txt

$mysite_hit_counter = ("total_save_domain_name_hits_counter.txt");

Step 2: reads the current number of hits

$live_hit_count = file($mysite_hit_counter);
$live_hit_count[0] ++;

Step 3: Open the file using PHP

$fp = fopen($mysite_hit_counter , "w");

Step 4: Update the value

fputs($fp , "$live_hit_count[0]");

Step 5: Close the txt file.

fclose($fp);

Step 6: display your hit number

$sno=$live_hit_count[0];
echo $sno;

Simple Web Page Hit Counter Code Using PHP

index.php

<?
$mysite_hit_counter = ("total_save_domain_name_hits_counter.txt");
$live_hit_count = file($mysite_hit_counter);
$live_hit_count[0] ++;
$fp = fopen($mysite_hit_counter , "w");
fputs($fp , "$live_hit_count[0]");
fclose($fp);
$sno=$live_hit_count[0];
echo $sno;
?>

html visitor counter source code

index.php

<?
// simple PHP start at the top of the page since i start a session
session_name('mysite_hit_counter');
session_start();

$store_file_data = 'total_save_domain_name_hits_counter.txt';
$live_hit_count = 0;
// here online read current hits
if (($live_hit_count = file_get_contents($store_file_data)) === false)
{
	$live_hit_count = 0;
}
// here simple write one more hit
if (!isset($_SESSION['page_visited_already']))
{
	if (($fp = @fopen($store_file_data, 'w')) !== false)
	{
		if (flock($fp, LOCK_EX))
		{
			$live_hit_count++;
			fwrite($fp, $live_hit_count, strlen($live_hit_count));
			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 - www.pakainfo.com</title>
</head>
<body>
<p>Total Page content...</p>
<div class="counter">
	<p>This Web page has <span><?=$live_hit_count;?></span> hits</p>
</div>
</body>
</html>

Hit Counter Usage

Question: What would a PHP code site be without a hit counter?

Read Also:  How to $state.go() in Angularjs?

Therefor We are throwing in my latest php version of this full source code.

Using php function with PHP File Handling like a name as a file_get_contents() for fast reading of the hits and save total counter,
implements php a session as not to record a hit for the same ip address to user again
as well as this web page or file locking for safe with multiple writes.

Of course use as is since simple flock() can block the full source code execution or run if
you have many more spamming or lots of the visitors! For any non Windows users you can use a non-blocking
flock():

if (flock($fp, LOCK_EX | LOCK_NB))

after that you can run a timed using loop to wait for some wait like as a milliseconds before aborting the locking.
Obviously that would be an overkill of a tiny hit counter.