Creating Your Own PHP Web Site Hit Counter

Creating Your Own PHP Web Site Hit Counter

Hey there! Today I’m going to teach you how to make a cool hit counter for your website using some super simple PHP. This counter will help you keep track of how many times people visit your site. It may sound complicated, but trust me, it’s easier than you think!

Now, before we dive in, I want to let you know that we won’t be dealing with any fancy stuff like MySQL. Nope, this tutorial is all about using a simple flat file database. So no worries there!

If you have your own website and it allows you to run PHP (which most hosting providers do), you’re good to go! We’ll create an easy-to-use hit counter that anyone can understand.

Before we get started, I just want to mention a few things:

  • This hit counter is about as basic as it gets. No fancy statistics or link tracking here, just simple counting.
  • You’ll need to know how to log in to your web server using FTP. Don’t worry, I’ll walk you through it!
  • For this tutorial, we’ll be using Filezilla, a free FTP client that works on Windows, Mac OS X, and Linux.
  • I’ll be assuming that you’re using Windows, just to keep things simple.
  • It’s a good idea to create a folder on your local hard drive called C:counter to keep track of everything.

Step 1. Create the counter directory under the public directory on your web server.

The public directory is usually called public_html on most servers. Inside that directory, create a new folder called counter using Filezilla.

Here’s what it should look like:


The directory, since it’s new, is clearly empty because you haven’t put anything in it yet.

Step 2. Make a flat file database on your computer.

You can easily do this by using Windows Notepad, as the database itself is just a text file.

Create a text file with only one line:

It should look like this:


Hey there! I’m here to help you with the next steps. So, first things first. Save the file as c:counterdb.txt. Easy peasy, right?

Now, let’s move on to Step 3. We need to upload db.txt to the counter directory on your web server. Don’t worry, it’s not as complicated as it sounds.

Open up Filezilla and find your way to the c:counter directory on your computer. Then, navigate to the counter directory on your web server. Now comes the simple part: just drag and drop the file to upload it. Ta-da!

And voil√†! Once you’re done, it should look something like this.


Step 4. Make sure db.txt on the web server can be modified by anyone.

The database (db.txt) needs to have the right permissions so that it can increase a number each time someone visits it.

To do this, you’ll need to right-click on the db.txt file on the web server and choose File Attributes from the menu that comes up. In the window that appears, check all the boxes and click OK.

It should look like this:


So, here’s the deal. If you want to make a file world-writeable, there’s a couple of things you need to do. First, you want to make sure that the permissions are set correctly. In this case, we’re looking for permissions listed as -rwxrwxrwx (including the dash).

Now, let’s get to the nitty-gritty. We’ve got a script that you need to follow. It’s pretty straightforward, so don’t worry. Here are the steps:

Step 5. The counter script itself.

$db = “db.txt”; // The database we created

$handle = fopen($db, ‘r+’) ; // Open the db, read and be able to write to it

$data = fread($handle, 512) ; // Get current count from the db

$count = $data + 1; // Add in a count since it’s being read

print $count; // Display current count on the web page

fseek($handle, 0) ; // Point back to the beginning of the file

fwrite($handle, $count) ; // Save the count

fclose($handle) ; // Close count

Now that we have the script, it’s time to put it all together. Here’s what you need to do:

First, copy the text above and paste it into a new text file using Notepad. Make sure to save the file locally as c:countcounter.php. Keep in mind that you might need to adjust the file path to match your system.

Finally, it’s time to upload the counter.php file to the counter directory. Once you’ve done that, you’re good to go!

Alright, that’s it! Looks like we’re all set. Good luck with your world-writeable file!


You don’t need to change the permissions.

Step 6: Run the PHP script.

To run the PHP script, all you have to do is load it in a web browser. Just replace “” with your own website domain and see what happens.

What’s supposed to happen is that you’ll only see the number 1. If you refresh the page by pressing F5, the number will change to 2. Keep pressing, and it’ll keep increasing to 3, 4, and so on.

Step 7: Embedding the script in other PHP web pages.

If you want to display the script in other web pages, you can do so by using the include function.

To test this, let’s create a quick PHP web page.

Open Notepad and enter the following:

Save this file as “c:countertest.php”.

Upload “test.php” to the counter directory.

Now, run “test.php” in a web browser (load it as “”).

If everything goes well, you should see the message “Hello, this is my test web page” with the count number underneath it.

It will look something like this:

…and that’s all there is to it.

Leave a Comment

Do not miss this experience!

Ask us any questions

Get in touch