TechJunkie is a BOX20 Media Company

Home Web Creating Your Own PHP Web Site Hit Counter

Creating Your Own PHP Web Site Hit Counter

In this article I’ll show you how to use really simple PHP so you can create a free hit counter for use on your web site to track hits on web sites in a very basic sense.

This script tutorial uses a flat file database so you don’t need to mess around with MySQL or anything like that.

[hidepost=1]

If you host your own web site and have the ability to run PHP (which the vast majority of web host providers do allow), you can create an easy-to-use basic hit counter.

Notes before continuing:

  • When I say basic, this is as basic as hit counters get. No statistics, no link tracking, none o’ that. Just plain basic counting.
  • You do need to know how to login to your web server via FTP.
  • For this tutorial the FTP client being used is Filezilla, a freely available client that can be used on Windows, Mac OS X or Linux.
  • It will be assumed you are using Windows during this tutorial.
  • It is suggested you make the directory C:counter in your local hard drive to keep track of where things are going.

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

The public directory is usually labeled as public_html on most web servers. Under this directory, make a directory called counter using Filezilla.

Looks like this:

image

The directory, being new, is obviously empty since you have nothing in it yet.

Step 2. Create your flat file database locally on your computer.

You can do this with Windows Notepad as the database itself is nothing but a text file.

Create a text file with just one line in it:

0

That’s a zero, not letter O. Save as db.txt

Looks like this:

image

Save as c:counterdb.txt

Step 3. Upload db.txt to the counter directory on your web server.

Using Filezilla, navigate to your c:counter directory on your local computer and the counter directory on your web server. Then upload the file. You can do this by simple drag and drop.

Looks like this once finished.

imageStep 4. Modify the permissions of db.txt on the web server to “world writeable”.

The database (db.txt) needs to have permissions set on it so it can count up a number each time it’s “hit”.

Right click the db.txt file on the web server and select File Attributes from the menu that appears. From the window that pops up, check all the boxes and click OK.

Looks like this:

image

You will know you’ve successfully enabled a world-writeable file if the permissions are listed as -rwxrwxrwx (including the dash), like this:

image

Step 5. The counter script itself.

<?php

$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

?>

Copy the above text into a new text file in Notepad.

Save the file locally as c:countcounter.php

5. Upload the counter.php file to the counter directory.

Looks like this:

image

The permissions can be left as is.

6. Run the PHP script.

You run it by loading it in a web browser. Load up

www.your-site.com/counter/counter.php

…replacing your-site.com with your web site domain and see what happens.

What should happen is that you will only see a number 1. If you refresh the page by pressing F5, it will change to 2. Press again it will change to 3, 4 and so on.

7. Embedding the script in other PHP web pages.

You can embed the script to display in other web pages by using the include function.

We can test this by creating another quick PHP web page.

Open up Notepad and enter the following:

<html>
<head>
<title>test web page</title>
</head>
<body>
<h1>Hello, this is my test web page.</h1>
<p>
<?php include("counter.php");?>
</p>
</body>
</html>

Save this file as c:countertest.php

Upload test.php to the counter directory.

Run test.php in a web browser. (load as http://www.your-site.com/counter/test.php).

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

Looks like this:

image

…and that’s all there is to it.

[/hidepost]

Cleaning Up Spoken-Word Recorded Audio With Audacity

Read Next 

Leave a Reply

Your email address will not be published. Required fields are marked *


Adam

Nov 19, 2008

643 Articles Published

More