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


/* 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, 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.




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.

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


    1. Nick
      Nick Published |

      Do you have a code where I can copy and paste into my PHP page then it works !

  2. jose cantu
    jose cantu Published |

    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

  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.


    1. Hi
      Hi Published |

      in the code where it says something like $count + 1, replace it with $count + 2

  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?

  5. Rahul
    Rahul Published |

    I am trying to use the code on my company’s website having HTML pages, do I need a different type of server or modifications in server?

    1. Max
      Max Published |

      It won’t work on an HTML page. Firstly, you need to have a PHP page. Secondly, you need to have a php installation on your server. Thirdly, your server has to be configured for PHP. See for instructions on how to do so.

  6. Glenn
    Glenn Published |

    This doesn’t run from a file that ends in “.html”. Do you have another way to do this from an HTML page?

    1. l3szcz
      l3szcz Published |

      Hi, use an ajax script (POST to PHP script) 🙂

  7. luke
    luke Published |

    For those that aren’t counting up. Likely your server space does not have permissions for 3rd parties (anyone looking at the page) to update your files, ie the counter file won’t be updated. To fix this right click the counter txt file and under properties change the permissions so others can both read, write and execute the file

  8. luke
    luke Published |

    And there is no way to do it in html, unless you want to get into java script. Simply change your file from file.html to file.php. Php can read all the html, nothing will change, but the counter will work

  9. Andy
    Andy Published |

    Thx, I’ve been looking for something like this for a while.

    Much appreciated 🙂

  10. Rajinder Singh
    Rajinder Singh Published |

    Thanks, you have solved my problem today!

    The numbers are increasing on page refresh, could you please provide the code that increases the numbers on unique visitors?

    I will appreciate. Thanks in advance!

    1. Milena F
      Milena F Published |

      Hallo Rajinder Sing,

      you can do it in a lot of way,

      you can put something on client side, for exemple in local storage and

      do something like

      if (! this key – value in local sorage ){

      execute this scrpit

      set a key – value in local storage

      this allow you for unique visitor, at least till him/her don’t clean local storage

      or you can get the ip of users and store it in another file,

      but here there’s a problem, if user change ip, look like e new user

      if another user get the same ip will not be counted

      maybe they could work in conjunction

      in this way new user without local sotrage is conted

      and a old user with different ip , but with local storage setted,

      or that have cleaned the storage but not changed ip,

      will not be conted,

      this reduce a little duplicated user


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.