Photoshop Tutorials, Flash Tutorials, 3Ds Studio Max Tutorials, Web Design Tutorials


Software Tutorials

Featured Free Templates
View All Templates!
Preview  |  Download
Name: metamorph_flame
Downloads:
Preview  |  Download
Name: metamorph_skyandclouds
Downloads:
View All Templates!
Recommended Hosting:
Host Unlimited Domains on 1 Account
1500GB storage and 15000GB bandwidth for $6.95/mo!
Recommended Hosting:
Host Unlimited Domains on 1 Account
1500GB storage and 15000GB bandwidth for $6.95/mo!
:: Free Website Templates
Preview  |  Download Preview  |  Download Preview  |  Download
Name: metamorph_sunset Name: metamorph_flower Name: metamorph_waterfall
Downloads: Downloads: Downloads:

Do You Like Our Website? Share With Others!
GoogleGoogle slashdotYahooMyWebDiggTechnoratiDelicious
Page: 12345678910111213141516171819202122232425

Welcome To WebDesignTutorials.net Flash Tutorials Area - Loading External Images

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.

Step two: Creating the Flash Movie

  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.

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:

  • Gets the PHP script to run, which increments the count number.
  • Reads the new count number.

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

Click to enlarge
Giving the Text Box a Variable Name in Flash. (Click to enlarge)

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:

  • Flash calls the PHP script into action.
  • The PHP script reads the Text file.
  • The PHP script adds one to the Text file number.
  • The PHP script saves the updated Text file.
  • The PHP file displays the new count number.
  • Flash reads this new count number.
  • Flash displays the new count number in the Text box.

Step five: Up Loading the Files

You should now have the following 5 files:

  1. You now need to upload all the files except for the Fla file:

    Note: All the files need to be in the same folder on your web server.

  1. When you have uploaded all the files you have to change the CHMOD setting for the text file: .

    This is so that the PHP script has the correct permission to save to the text file. Change the setting to: 666

    CHMOD set to 666 for the Text file: PHPCounter.txt
  2. You are now ready to test your Flash counter. Go to Internet Explorer (or other Browser) and type in the full web address for the file:

    Note: Unless you have a PHP Server on your own computer (which is unlikely) you will not be able to test this file from the web page saved on your local hard drive. Testing the file in Flash will not work either. You have to save all the files to a PHP enabled Server and then test them. This is why you upload and then test the web page directly from the web server (which has to be PHP Enabled).

    That is all there is to it. Your Flash Counter should now be happily counting away.

Author: Phil

Advertisements
Graphic Software, Flash Templates, Free Stock Photos, Web Templates, Web Design, Corporate Web Design, Royalty Free stock Photo, Affordable Website Design, Submit site, Add Url, Stock Photos, Survey Software, Anti Virus Software, E mail software, Computer Software

Premium Partners
  Free Website Templates - Flash templates, Affordable Website Design, Website Templates, Website Redesign, Custom Website Design, Web Design Tutorials, Flash Tutorials, Promotion Tutorials - that is what we do. Metamorphosis Design Studio offers quality, free and low cost web site templates for your business and personal life, we also offer affordable web design and site re-design.
  Vertex Website Tempales - It saves tones of time and money to use pre-made web designs to build your web site. You need a web site but you don't want to pay thousands dollars to professional web design companies? Our web templates is just for you! They are designed to be easilly edited by your favorite html editor, like MS FrontPage
  Photoshop Tutorials - Learn how to build stunning web pages using Adobe Photoshop, Macromedia Flash MX and 3D Studio Max software. The step-by-step approach makes it so easy, that even beginners can produce great web pages. Get started with these tutorials covering everything from page layout to content tips.
Free website templates and paid web templates are great tools to make your websites look perfect! You will save time and money with our flash templates and free website templates Our visitors are satisfied with the quality of our free and paid website templates! Please visit our free website templates and paid website templates sections. We offer free web templates, free web layouts, free web page templates and other stuff for free download. All templates come with the html and external css file so you may easily edit HTML with your favorite HTML editor. Feel free to download our free web templates for your personal websites.

Terms of use depend upon the website template vendor.
Home  |  Submit Tutorial  |  Top Sites  |  Free Templates  |  Website Templates  |  Privacy Policy  |  Contact Us
All Right Reserved by WebDesignTutorials.net