Learn how to create a hit counter in Flash with the aid of PHP.

Example of Flash - PHP hit counter

Note: To test the hit counter above press the Refresh Button at the top of your Browser:  (Usually F5).

The count number should go up by one.

Step one: A PHP Enabled Server & CHMOD Settings

If you have a PHP Enabled Sever and can change the CHMOD settings of your remote files please go to the next step.

  1. Open a new: Flash Movie
  2. Go to: Modify > Document
  3. Set the size to: 350 x 50 pixels
  4. If you wish select a: Background Colour
  5. Click: OK
  6. Select the Text tool:
  7. On Stage drag out a: Text Box
  8. Return to the Selection tool:
  9. If the Property Inspector is closed, open it: Window > Properties (Ctrl F3)
  10. In the Property Inspector set the Text Box to: Dynamic Text

    Selecting Dynamic Text.

  11. Give the Text Box a Variable Name (Var): myCount

    Click to enlarge
    Properties for the Text Box. (Click to enlarge)

    Note: In the Property Inspector above the text alignment is set to right. This is normal for numbers, but not essential.

  12. Create a new Text Box and add other text that you may want. I added: This Flash Movie has been viewed: times

    Note: The space in the sentence above is to give room for the count number to appear.

  13. As your first text box was set to Dynamic Text the next Text Box will take on this attribute. Reset the new Text Box to: Static Text

    Two Text Boxes sitting one on top of the other.

  14. Select Frame 1 in the timeline and add the following ActionScript (you may leave out the gray comments):
    // loadVariablesNum loads an external file into Flash as a variable
    // PHPCounter.php is the name of the external file
    //Zero is the level number


    loadVariablesNum ("PHPCounter.php", 0);
  15. The Flash Movie is now finished so make sure you go to: File > Save (Ctrl S)
  16. Name your file: PHPCounter
  17. Then go to: File > Publish > Publish Settings
  18. Under formats select: Flash and HTML
    File Formats.
  19. Click: Publish
  20. Click: OK

    Note: Publishing creates an SWF file (Shockwave file or Flash Movie) and an HTML file (web page). The web page will automatically have the Flash Movie visible on the page.

Step three: Creating a Text file to store the Count Number

You will need a very simple file which stores the current count number.

  1. You will need to open a Plain Text Editor:

    PC: Go to: Windows Start Button > Programs > Accessories > Note Pad

    Mac: Open: Applications> Simple Text

  2. Type the number following: myCount=0

    Note: The start number is zero so that the first time the page is viewed it will go up to 1 and the Flash counter will display 1 not zero. Of course you could start your page count on any number you wish. Remember - Don't believe everything you read. Just because a web site boosts that there have been a trillion visitors since yesterday - well it's only as trust worthy as the site itself. Of course I never doctor any of the webwasp page counts (I'm telling the truth !!).

  3. Save your file to the same location as your Flash Movie that you Published earlier and use the file name: PHPCounter

    Note: Because you are in a Text Editor it will automatically add the standard file extension. So your file will actually be called: PHPCounter.txt

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):
    <?
    // 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";
    ?>

  3. 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.

Author: Phil