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


html visitor counter source code

index.php






PHP Hit Counter Example Code - www.pakainfo.com


Total Page content...

This Web page has hits

Hit Counter Usage

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

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.

Leave a Comment