The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.
The Flash and PHP Bible has a dedicated forum for support and comments.
How to write a visitor counter in PHP.
<?php
$txt_file = "counter.txt";
if(file_exists($txt_file)) {
$f = fopen( $txt_file, 'r+' );
$count = fread($f, filesize($txt_file));
rewind($f);
$count++;
fwrite($f, $count);
ftruncate($f, ftell($f));
fclose( $f );
} else {
print "File Not Found";
}
$s = $count;
$line = sprintf("%s people have visited the site.", $s);
print $line;
?>
$txt_file = "counter.txt";
This variable is the text file.
if ( file_exists($txt_file) )
{
If the file is found continue, or print an Error.
$f = fopen( $txt_file, 'r+' );
Open the text file so we can read out the count value.
$count = fread( $f, filesize( $txt_file ) );
Read the text file, and fill the $count variable with the result.
rewind( $f );
Move the pointer to the beginning of the file
$count++;
Increment the $count value by "1".
fwrite( $f, $count );
Write the new number to the text file.
ftruncate( $f, ftell( $f ) );
Get rid of any extra space in the file that might exist from writing to it.
fclose( $f ); }
Close the text file, since we are finished with it.
else
{
print "File Not Found";
}
If the file was not found print an error.
$s = $count;
Move the $count variable to a new variable "$s".
$line = sprintf("%s people have visited the site.", $s);
Create a string that will print to the user, displaying the new $count number.
print $line;
Last but not least, we print the line back to the user.
That is the end of the counter tutorial. Now you should have a better understanding on it.
|
Robert S. Sat Feb 24, 2007 11:08 pm
Thank you for the code. I will let you know how it goes.
|
|
mkeefe Sun Feb 25, 2007 12:16 am
Glad you liked the tutorial, it is always nice to hear back from the readers.
|
|
Cy Mon Mar 26, 2007 2:16 am
I have to say thank you here. I wanted a quick solution for a SIMPLE counter. And you provided it. Thank you so much. =)
|
|
atari games Tue Apr 10, 2007 8:15 am
best good site. thanks
|
|
Adnan Sun May 20, 2007 4:08 pm
yes really nice work. thanks buddy
|
|
Pandian Thu Jun 14, 2007 4:07 am
In a high traffic site, Two or more file write process into file may collide and get lock with each other..
|
|
Nemanja Thu Nov 27, 2008 7:42 am
How to show this counter in my flash site (action script 3), or how to create a counter with action script 3
|
©2004 - 2008 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN