How to create visitor PHP Hit Counter for your website?

User avatar
Posts: 53
Joined: Mon Jan 30, 2017 4:58 am

How to create visitor PHP Hit Counter for your website?

Tue Aug 14, 2018 8:54 am

This is a basic site hit counter. What it does is take a few lines of code that open up a file called (countlog.txt) and changes a simple number in it.

First off, what you need to do is make a new (.php) file in your text editor and place this code in it. The only thing you may need to change is the parts of the code that says (/countlog.txt). That’s only if you plan to put your files in a different directory. I put mine in my ROOT directory, so I put the (/) in front of (countlog.txt).

Code: Select all


/* counter */

//opens countlog.txt to read the number of hits
$datei = fopen("/countlog.txt","r");
$count = fgets($datei,1000);
$count=$count + 1 ;
echo "$count" ;
echo " hits" ;
echo "\n" ;

// opens countlog.txt to change new hit number
$datei = fopen("/countlog.txt","w");
fwrite($datei, $count);

Save the file as (counter.php). Now, onto the next part. Simply make a file named (countlog.txt) and put a (0) in it. Then save. Or, you could put whatever number you want to in the file. This is where you counter will start counting from. If your last hit counter was already at 22,000 hits, why start over? Just type in (22000) in the (countlog.txt) file!

To include the file in any page in your site all you have to do is include it. Here’s the code.

Code: Select all



Of course, you may have to change the path on that as well, depending on what directory you put your file in. Or, you could just put the (counter.php) code inside of any page you want. It will work just the same.



In total there are 10 users online :: 1 registered, 0 hidden and 9 guests
Registered users: Google [Bot]
Most users ever online was 33 on Sat Mar 09, 2019 8:21 pm
Total posts 489
Total topics 371
Total members 70
Our newest member Mirasoida
No birthdays today