Write a comment on this article

A simple counter

< back to tutorials main page

Who doesn't know it? A simple counter on a website which displays the number of visits. With this tutorial you will make the first step to create your own counter.

First things first: the number of visits has to be saved consistently, this for we use a plain textfile. So we need two files: one to display and update the visits (index.php) and one to save the number (count.txt).

Because we need to read and write the file count.txt, it needs appropriate attributes. If the webserver is running on a Windows machine, you need to give write permissions on count.txt to the user that runs the webserver (rightclick on the file and select the security tab). Note: if your filesystem is not NTFS, you don't need to/can't make set the security. If the webserver is running on a Unix machine, you need to run the command "chmod" with the octal number 666 (read and write) on the file count.txt.

count.txt
This file will contain one number only. At the beginning it obviously should be 0.

index.php
Here comes the programming part :) This file can of course contain HTML structures as well. It can also be embedded in another PHP file with the use of the functions include and require.

At the beginning we define a variable which contains the name of the counter file (because we need it more than once):

$file = "count.txt";
 

The rest is split into three parts:

  1. Reading the file.
  2. Increment and print the count of visits.
  3. Writing the file.

To read the file we need to open it, read its content and close it agian:

$handle = fopen($file,"r");      //open file
$count = fgets($handle,1000);    //read the content
fclose($handle);                 //close file handle
 

Afterwards we increment (add 1) and print the visit count, which is now stored in variable $count, with one line of code:

echo ++$count;    //increment and print count
 

Finally we write the new count into the file. So we open it again, write the value and close it. Because we write the file we should lock it once we opened the file handle. This will prevent some awkward surprise:

$handle = fopen($file,"w");    //open file
flock($handle,LOCK_EX);        //lock for writing
fwrite($handle,$count);        //write value
flock($handle,LOCK_UN);        //unlock file
fclose($handle);               //close file
 

That's it! With ten lines of code we've just created a simple counter.

You can try this script here: demos/A_simple_counter/index.php. Or download it here: A_simple_counter_1-0.zip

< back to tutorials main page

Tags

PHP, tutorial, counter, file, read, write

Comments (0)

Leave a comment

If you have a question or a problem and you're looking for a solution, please use the forum. Comments might not be answered.

Your name (required, maximum 255 characters) ( remember)
Your message (required, maximum 5000 characters)
 
Insert these letters into the textfield below: YZJH (required, case-sensitive)
Drag the slider to the right (required)