Building Counters
For this tutorial we will show you how to build a simple counter for your website .
The basis of a counter is the same in most languages , create a text file to hold the count , then we read the file , increment the count , open the text file , store the new count and then close the file .
<?php
//this is our text file if you create a different
//named file change the script below to reflect this
$counter_file = ("mycount.txt");
//now we open the file
$visits = file($counter_file);
//this increments the counter value by 1
$visits[0]++;
//now we will open the counter file for
//writing "w"
$fp = fopen($counter_file , "w");
//put the new count value into the counter
//file
fputs($fp , "$visits[0]");
//close the file
fclose($fp);
//display the count
echo $visits[0];
?>
And to add this to your page you enter the following where you wish the counter to appear , again if you have called your file with the script in it something other than mycounter.php , you will have to change the script to reflect this.
<?php
include ("mycounter.php");
?>
Here is the count displayed on this page.
3586
In part 2 we will build a graphical counter and give you a couple of locations for a large amount of counter digits
Forward to Part 2
|