|
||||||
|
||||||
Tutorial - Hit Counter using PHP & Flash
Free
Flash PHP Tutorial
Step Four: Creating the PHP File
If you remember the line of ActionScript in your Flash Movie
refers to a file called: PHPCounter.php This
is the file you are about to create and that the Flash Movie actually reads.
The Flash Movie does not read the previous text file which actually stores
the count number. At first this may seem strange but I will explain why after
we have looked at the PHP code.
<?
// Creates a variable which remembers the file name that has the count number in it.
$filename = "PHPCounter.txt";
// Open the text file
$fp = fopen( $filename,"r");
// Reads the text file
$Old = fread($fp, 100);// Closes the text file
fclose( $fp );
// Ignores the texts and gets the number following the equals sign
$Old = split ("=", $Old, 5);
// Add 1 to the current number
$NewCount = $Old[1] + '1';
// Creates a variable called NewCount to remember the new number
$New = "myCount=$NewCount";
// Opens the text file
$fp = fopen( $filename,"w+");
// Locks the text file so that other programs cannot write to it.
if (flock($fp, 2)) {
// Write the new number
fwrite($fp, $New, 100); }
// Closes the text file
fclose( $fp );
// Display the new count on the PHP web page
print "myCount=$NewCount";
?>
How the PHP Script Works
This last line of the PHP script could be confusing. What is this web page?
The PHP file you are currently working on is a web page. It is just like an HTML web page except it is dynamically created by the script above and simply displays the count. This page is not designed for human eyes but for the Flash file to read.
The number you see will be at least one higher than the Flash Counter at the top of this page because by clicking on the link the PHP script runs again and thus adds one to the count. If someone else has viewed the page since you first opened this page the number will be yet higher again.
So it is this PHP file that the Flash Movie reads not the text file that stores the count number! You could easily get the Flash file to read the text file. In the Flash file you would simply replace the swap the name of the line of ActionScript to point towards the text file like this:
loadVariablesNum ("PHPCounter.txt", 0); // Do not do this!!
Although this would accurately read the count number because the PHP script has not run the number would never get any higher. So the counter would not count. So by getting the Flash Movie to read the PHP file you are achieving two things at once:
If you clicked on the PHPCounter.php link above you will have noticed that the PHP web page displays something like this:
myCount=1745
Flash cannot handle information unless it is contained in a variable. In this case the variable is called: myCount
You will remember that when you created the Flash file you gave the Text Box on stage the Variable name: myCount

Giving the Text Box a Variable Name in Flash.
Because the Variable Name of the Text Box and the Variable Name that Flash reads off the PHP file are the same Flash Because knows to display the number in the Text Box that we placed on the Stage. In fact Flash will display anything placed after the equals sign. In this case that is a number.
So to sum up:
|
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
60604 visitors to this page since
Nov 04 •
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|