»»»
A simple counter using php
This quick example shows you how to display the browser
information of a visitor to your website using php.
It's very basic. You should 1st create a file to store
the hits in, in this example we used a file called 'mycount.dat'
& started the count at 100. You can start it at
1 or 1 million, up to you.
Here is the script for this
example
<?php
//A simple counter using php
//open the counter file in read only mode
$counterfile = "mycount.dat";
if(!($fp = fopen($counterfile,"r"))) die
("cannot open counter file");
//Check the value stored in the file
$count = (int) fread($fp, 20);
//close the file
fclose($fp);
//increment the count
$count++;
//display the count
echo "Welcome visitor no : $count";
//open the file again in write mode (w) and store
// the new count
$fp = fopen($counterfile, "w");
fwrite($fp , $count);
//close the file
fclose($fp);
?>
And here is the output , refresh
your browser to increment the count.
»»»
Welcome visitor no : 2232
Warning: fopen(mycount.dat) [function.fopen]: failed to open stream: Permission denied in /home/flashgim/public_html/tutorials/php_tutorials/motionrush_3_php_tutorial/index.php on line 202
Warning: fwrite(): supplied argument is not a valid stream resource in /home/flashgim/public_html/tutorials/php_tutorials/motionrush_3_php_tutorial/index.php on line 203
Warning: fclose(): supplied argument is not a valid stream resource in /home/flashgim/public_html/tutorials/php_tutorials/motionrush_3_php_tutorial/index.php on line 205
You can drop this snippet anywhere
you like, but the page you put it into must be able
to parse PHP code. So it should end in *.php