Training: PHP Flash Hit Counter - The PHP File

Home • Members Tutorials Forum iSnapChat Contact Us 

 


Flash Tutorials

   

Tutorial - Hit Counter using PHP & Flash

 

Free Flash PHP Tutorial


Step 4    <<   Previous      Intro   1   2   3   4   5     Next   >>      

 

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.

  1. If you have closed your Text Editor re-open it or if your Text Editor is still open go to: File > New
  2. Type the following code (you may leave out the gray comments):
  3. <?
    // 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";
    ?>

  4. Save the file in the same location as the previous files as: PHPCounter.php


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: