Introduction
This tutorial shows you how to create a simple counter that you can use anywhere in your flash movie. The most obvious place is for either the intro or the main part of the Flash Movie that you are working with. You can also use this in other parts of your movie as well. For example, say you needed to keep track of how many users are viewing a certain section of your website, you can add one line of code and one text file - and you'll be able to keep track of that section. This can be advantageous because sometimes it's good to know what users are viewing and what the most popular sections of your website are - if for no other reason then just making sure that you've got that section designed as good as possible. Another reason might be that you need to place a banner ad or other form of advertisement on the most popular section of your website. If you happen to visit message boards where you can use a Flash Footer - you can also add this to that to keep track of how many people are viewing your posts - not that useful though except as a gimic.
Included Files:
- PHPCounter.fla - the Flash source code.
- PHPCounter.php - The php Source Code
- PHPCounter.txt - The text file where you store the current count (Site Visits) - be sure to change the permissions to 777 on this.
Overview
This Tutorial shows you the basics of creating a simple Counter for your flash website. You can use this same technique to keep track of almost any part of your website. Say if you want to know how many people go to each section - with a few modifications and copy and pastes - you'll have it all set up in no time at all.
For a working example Click Here - This just keeps track of how many people have clicked that link you just hit - you can also refresh that page and see the counter increase by 1.
Requirements: PHP 4 installed on your server. Some knowledge of Actionscript.
Part I - Setting up the Flash Movie
This is the easy part - basically all you need is a simple loadvariablesNum command and a dynamic text box. You can either open up the attached download file, or you can just make a new Movie in Flash. Either way will work - this part is really not that complicated.
Create a Dynamic Text Field named Count in the Flash movie. This is illustrated in the below image:

In the First frame of the Movie add the following actionscript:
loadVariablesNum ("PHPCounter.php?ran="+random(9), 0);This is illustrated Below - By adding the random(9) onto your site you create a unique file each time, if you left this out you would load the same php script that is in the cache and the file would not be updated - you may want to increase the 9 to 99 or even 999 - it doesn't matter for this tutorial or this counter in general:

You do not technically have to go any further then this. You can make this into a Live counter however with the following Steps.
1) In the second frame add this line of actionscript:
loadVariablesNum ("PHPCounter.txt?ran="+random(9), 0);2) Wait to about frame 50 or however long you want to wait between updates and Loop back to Frame Number 2 with the following actionscript: gotoAndPlay (2); in that frame. This will reload a fresh version of the text file that contains the current number of visits. If there have been any more visits since the last time the text file was updated it will increase the value in the dynamic text box named 'Count' by that number.
Part II - Setting up the PHP Script
For this part you can use the attached script by just adding it to the same directory as the PHPCounter.swf. Not much else has to be done. I'm going to go over the script in Detail however in the hopes that you'll have a better understanding of how it works and so that you can create your own modifications in the Future. (For example purposes Line numbers where used here for the example - do not use line numbers in the actual script.
1) <?
2) $filename = "PHPCounter.txt";
3) $fp = fopen( $filename,"r");
4) $Old = fread($fp, 100);
5) fclose( $fp );
6) $Old = split ("=", $Old, 5);
7) $NewCount = $Old[1] + '1';
8) $New = "Count=$NewCount";
9) $fp = fopen( $filename,"w+");
10) if (flock($fp, 2)) {
11) fwrite($fp, $New, 100);}
12) fclose( $fp );
13) print "Count=$NewCount";
14) ?>Line 1 - Tells the server to start processing this script as a PHP script.
Line 2 - Sets the variable filename to the name of the text file that we are going to be using to store the number of visits or Counts. Remember in PHP whenever you see a $ that indicates a scaler variable.
Line 3 - Opens the text file for reading.
Line 4 - This line basically places the content of the text file into the variable Old.
Line 5 - Closes the text file.
Line 6 - This line splits the text file where ever an = sign occurs and puts the two parts into an array that can be referenced as follows: $Old[0] references the Count part of the string, $Old[1] references the Number part of the string. This is important because we only want to read and add to the number and not the whole string (Count=Number).
Line 7 - Adds 1 to the current Count.
Line 8 - Creates a new string that can be placed into the text file. We need to add the 'Count=' part again because we got rid of it when we split the string into the array.
Line 9 - Opens the file for writing to.
Line 10 - An optional argument that locks the file (file locking) so that we do not write over the same file at the same time - this can cause the Count to reset and may cause other errors. This is mostly important if you think a large number of people will be visiting your site at any one moment.
Line 11 - This writes over the current text file with the New value - the count is increased by one.
Line 12 - Closes the text file for writing to.
Line 13 - Prints out Count=(The new Count) which is returned to the Flash movie - the value of Count then appears in the dynamic text box named Count in the Flash movie.
Line 14 - Ends the PHP script.
That's it for the script. The last and probably most important thing that your going to have to do is change the permissions of the Text file that your using - in this case: PHPCounter.txt - To 777 you can do this with Telnet by using the command: chmod 777 PHPCounter.txt in the directory in which it resides. You can also do this with some ftp programs such as ws_ftp.
Conclusion
That's about all there is to making a Flash Counter for your Flash Movie. You can use this same script with small modifications to do a wide variety of tasks.
-Jeffrey F. Hill
http://www.snowvids.com
| » Level Intermediate |
Added: : 2001-08-23 Rating: 7.98 Votes: 128 Hits: 5431 |
| » Author |
| No details available. |
| » Download |
| Download the files used in this tutorial. |
| Download (54 kb) |
discuss this topic to forum
