Keep track of your visitors!

Hey! today im going to talk you through the implementation of a simple hit counter using PHP.

Before we start!

What do you need?

In order to use this tutorial you must have access to a web hosting account that has PHP enabled. If you do not have this check out our dynamic hosting package, it provides everything you will need.

This tutorial assumes you have FTP access to your website and that you are familiar with uploading and changing your website files. If what I just said seems like utter jibberish to you head on over to our support section and read Uploading files to your starter or dynamic hosting account.

What is PHP?

PHP stands for ‘PHP Hypertext Processor’ (I know that doesn’t really answer the question, does it!?). It is a dynamic programming language designed for use on the web, it allows for the inclusion of dynamic (changing) content to me implemented into websites as opposed to standard HTML which only allows for static (non-changing) content. Fortunately PHP can be easily integrated into HTML so it’s not like you have to forget everything you already know and start from scratch. If you’d like more information on PHP check out it’s homepage at php.net.

What is a hit counter?

A hit counter is a script that you can include on your website to display the total number of visits that website has had. This is ideal for analysing website traffic or if like me you just like to show off how many visitors you’ve had.

Lets begin!

Step 1: Creating your ‘flatfile’

This is the file where your total number of hits will be stored, we call it a ‘flatfile’.

  1. Create a blank file called counter.txt (this can be done using notepad)
  2. Upload that file to your website (ensure that it is put in the same folder as the page on which you wish to display it on).
  3. Change the file permissions of that file to mode 644 (If you are hosting outside of LCN you may be required to set the file permissions to mode 777)

Step 2: Inserting the counter code

This is where we put our PHP into the website.

  1. Open up / Edit the webpage that you would like to display the hit counter on. (ideally do this with a text editor such as notepad).
  2. Input the following code where you like the hit counter to appear:
  3. <?php
    $countfile = ("counter.txt");
    $counts = file($countfile);
    $counts[0]++;
    $filep = fopen($countfile , "w");
    fputs($filep , "$counts[0]");
    fclose($filep);
    echo "<p>We've had $counts[0] visits!</p>";
    ?>

  4. You can edit the bottom section (the echo command) to display the visits any way you like. Just be careful not to delete $counts[0] as it’s the variable that displays the hits .

Once implemented you can style your counter anyway you like:

counter image

This tutorial covers the creation of a VERY basic counter. The code can be modified, adjusted and improved to display various other useful information so have a good play about!

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>