Return to Tutorials Index |
|
|
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 |
| Written by John Lavis |