Peritus Tutorials

Simple PHP (+ .txt) Hit Counter

by TrentonD on Dec.03, 2010, under PHP, Tutorials

When I was building my first websites, I always included a hit counter. I wanted a simple way to see how many people were visiting my site, and I wanted those visitors to have the ability to see this simple statistic as well. Because I didn’t like any of the “embed-me” hit counters available on the net at the time (offered through third-party websites and various web host control panels), one of the first things I did when I was learning PHP was create a simple, no-flairs and no-gimmicks, hit counter.

For this level of simplicity, this is the most common hit counter there is out there. It’s simple, it uses PHP and a text file hosted on your webserver. Let’s begin.

1. First, we must create a php file for our counter. Let’s call it hitcounter.php.

Because our hit counter will be written in php, it is necessary that your page is also a php file.

2. Next, we must place a text file on our web server that will track the number of hits our page gets. Open up Notepad (or any other text editor you prefer), and save a document with only the number “0″ (no quotations) as count.txt.

count.txt:

0

3. Now that we have our files ready to go, we may begin the coding process. We must start by inserting the opening PHP tag. We will also make a php variable for our count.txt file we created earlier.

<?php
$count = (“count.txt”);
We can now refer to our count.txt file at anytime on this page by using it’s variable $countfile. If count.txt is not in the same directory as hitcounter.php is in, make sure you reflect that. For example, if you put count.txt in a directory called “files” on your webserver, your code should read $countfile = “files/count.txt”.

4. Ok, now let’s add the part of the code that will grab the current number of hits, and then increment that number by 1.

$hits = file($count);
$hits[0] ++;

The first line creates a variable ($hits) that now contains the contents of $countfile (count.txt!) The second line then takes that variable (which will be 0, or whatever number you put in count.txt) and adds 1 to the first line.

5. Add some more PHP that will open count.txt in the script so we can physically change the current value of count.txt (0) to what we want (1).

$fp = fopen($count , “w”);

This line of code is using the “fopen()” php function to open our countfile on the webserver, in a mode that will allow it to write (w) to the file.

6. We have increased the hit count by 1, and we have our count.txt file ready to be written to.

fputs($fp , “$hits[0]“);

Here we are using the “fwrite()” php function to amend (or replace) the current contents of count.txt to our new $hit value.

7. All we have to do now is close the text file from within our php script, display the number of hits on our page, and then add the php close tag.

fclose($fp);
echo $hits[0];
?>

8. To get your hit counter to appear on your website, just paste the following code:

<?php
include(“hitcounter.php”);
?>

Note: because we are using a php include statement, any page that this is posted on must also be a php file. You can safely rename a .html file to .php without changing the way it displays in your browser as long as the global structure is still in place (your <html>, <head>, <body>, etc tags).

To see a full copy of the script we just created and other hints, tips and tricks, please click “continue reading” below.

count.txt:

0

hitcounter.php:

<?php
$count = (“count.txt”);
$hits = file($count);
$hits[0] ++;
$fp = fopen($count , “w”);
fputs($fp , “$hits[0]“);
fclose($fp);
echo $hits[0];
?>

I hope you enjoyed this simple PHP hit counter and more importantly, learned something from it! If you have any questions or comments please feel free to share!

Coming up: I will be creating a more “advanced” hit counter tutorial in a few days. It will use the same structure and method as this simple hit counter, but will be a bit more dynamic in the way that you can use it on multiple pages on your website without a hit on one page increasing the hit count on all others! Sit tight and stay tuned! :)

:, , , , , , , , , , , , , , , , , , ,

4 Comments for this entry

  • 10V

    $count = intval(file_get_cotents(“count.txt”));

    file_put_contents(“count.txt”, ++$count);

    • TrentonD

      Thank you for your input, 10V. That is a perfectly acceptable process as well. I just feel as if with a piece of code this simple, there really is no need to “complicate” it any further, but now the readers of this tutorial can try out both ways! Both variations will provide the same output.

      Thanks again! :)

  • stuart

    Hi, thanks for the code, How do I call hitcounter.php? i.e How do I add it to my page?
    Thanks

    • TrentonD

      Hi Stuart, you could either use a PHP Include to add it to your page, or simply take the actual code we wrote (in hitcounter.php) and put it on whatever page you would like to track hits.

      Hope that helps!

      Thanks for visiting and commenting :)

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives

All entries, chronologically...