PHP Hit Counter

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).

<?php

/* counter */

//opens countlog.txt to read the number of hits
$datei = fopen("/countlog.txt","r");
$count = fgets($datei,1000);
fclose($datei);
$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);
fclose($datei);

?>

Save the file as (counter.php). Now, on to 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.

<?php

include("/counter.php");

?>

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.

4 Responses

  1. Francene Williamson
    Francene Williamson Published |

    How do I put a limitation on the number of hits. I have a html page with a table.

    The first column is the HOST column. I have my hosts and each is a link to a registration form.

    When the registrant submits the form, I need a counter limiting 15 people to each host.

    When the 16th person tries to register, I need a message saying “closed” or “full”.

    How do I do this.

    New to this. Please help.

    Thanks!

    Reply
  2. jose cantu
    jose cantu Published |

    hi
    i´ve tried your script and it pretty simple but i cannot put a custom number of hits
    it always starts from 1
    is there something we need to do on the code???
    doesnt seem to be reading the file number

    Reply
  3. jimmy
    jimmy Published |

    Hi. I am using this code. But it increment the value twice. Like when i refresh the page first time it give 1 then again if i refresh it gives 3 then 5.. 7..9. and so on. why so ? Please help!!! Any help will be appreciated.

    Thanks
    Jimmy

    Reply
  4. Duds
    Duds Published |

    Thank you for this tutorial. I needed a very simple counter and this is great. Just wondering if there any way we can add commas to large digits?

    Reply

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.

css.php