6 visitors online
That Aston's back without a bingle

Lara Bingle's stolen $300,000 car has been found in Coogee.

2010-01-01
Read More
Road rage: man fights for life

NSW police say a man has been critically injured in a road-rage incident

2010-01-01
Read More
'Future al-Qaeda plots harder to foil'

Terrorists getting smarter about US defence systems, warns intelligence chief

2010-01-01
Read More
Kokoda Track claims second Australian

A Sydney man is the second Australian to die within a week while attempting the Kokoda Track in PNG.

2009-10-05
Read More
NSW leads way in job ads rise

NSW job ads rise for the third month in a row, providing further evidence that labour market is stabilising.

2009-10-05
Read More
Fifth Australian dies in Samoa

A 30-year-old woman confirmed as fifth Australian to die in tsunami.

2009-10-05
Read More
Australia foils terrorist plot to attack army base

MELBOURNE, Australia – Police in Australia foiled a terrorist plot for commando-style

2009-08-04
Read More
Pulled under car: victim unidentifiable

Woman who died was crossing at lights under green 'walk' signal, police say

2009-08-04
Read More
Thief thanks police for rescuing him

Italian thief was getting a tae-kwondo beating from his Korean victims when police intervened.

2009-08-04
Read More
Cheating husband's genitals glued

Three US women, who had affairs with a married man, accused of revenge plot with wife

2009-08-04
Read More
Hey Hey, iconic TV show might be back

A decade after it was consigned to television's dust bin, Hey Hey It's Saturday looks certain to return.

2009-07-25
Read More
Boffins work on synthetic brain

Research could offer unprecedented insight into the nature of consciousness and our perception of reality.

2009-07-25
Read More
Mobile camera plan to cut road toll

Covert speed cameras seen as a way of lowering the state's worsening road toll.

2009-07-25
Read More
CommBank 'losing war' against fraud

New "phishing" attack asks customers to make call to redeem "cashback bonus".

2009-06-02
Read More

 

Thursday, September 09th 2010

Login Register Panel

Username

Password

Forgot your password

Register

login panel Add To Favourites

Return to Tutorials Index

Small font
Large font

PHP stat counter

To make  simple stat counter in php is relatively easy.

Create a notepad and call it counter.txt and right click the notepad and got to security

Make sure that everyone can write to this file in the permissions or this code wont work.

We will assume you have some HTML and php knowledge before starting this tutorial.

Find the spot where you want your stat counter to appear in design view perhaps of your web editor and switch to code view.

Where the cursor is positioned type the opening tags for php

<?php

This stat counter doesnt connect to a database to record the information, it simply writes the information to a text file, suchas notepad.

$filename="counter.txt";

The start of the code denotes the variable name and what it equals, which is our notepad.

$fd = fopen ($filename, "r") or die ("Can't open $filename");

This next line opens the file in read mode hence "r" or dies if file not found or locked

$fcount = fread ($fd , filesize ($filename));

The content of the file is then opened by the fread and assigned the variable $fcount

echo "$fcount";

This prints the number onto your monitor

fclose($fd);

This closes the file

 

$fd = fopen ($filename , "w" ) or die ("Can't open $filename");

This next part re-opens the file in write mode (the text file must be globally writeable) or dies

$fcounted = $fcount + 1 ;

This bit assigns a variable fcounted = fcount = 1 meaning someone has just refreshed the page or arrived at your website and the code has counted them.

$fout = fwrite ($fd , $fcounted) ;

The count is then written to the text file.

fclose($fd);

And the file is now closed

?>

Below is the full code

<?php
 $filename="counter.txt";
 $fd = fopen ($filename, "r") or die ("Can't open $filename");
 $fcount = fread ($fd , filesize ($filename));
 echo "$fcount";
 fclose($fd);
 $fd = fopen ($filename , "w" ) or die ("Can't open $filename");
 $fcounted = $fcount + 1 ;
 $fout = fwrite ($fd , $fcounted) ;
 fclose($fd);
 ?>
 

Written by John Lavis