Hit Counter in Flash using PHP
by Syko
Introduction
A hit counter in flash is not hard at all. The only things
required for making a hit counter in flash is a computer,
Flash (preferably MX or higher), text editor (even notepad
will do, I use Dreamweaver), and most importantly a server
that supports php 3 or higher.
What we're going to do is
draw the appearance of the counter in flash, write a php
script that will store the visitor count in a small text
file, and then call the php script from flash. There are two
types of flash hit counters: a simple counter that increases
after refreshing the page or a live counter which refreshes
itself after a period of time.
[ A simple
live flash hit counter using php ]
Creating the Flash Movie
We just have to make a movie with a simple textbox in it
with a variable name of "count" and add some actions to make
it work.
-
First open up flash, draw a new dynamic
textfield onto the stage and change it's variable name to
"count" (without the double-quotes).
[ dynamic
textfield, var-named "count" ]
-
Now you have to decide whether you want to
make a simple boring counter or a LIVE counter that
refreshes itself! In case you want to make a live counter
read on, otherwise skip to step VI
-
Make two new keyframes on your timeline,
one on frame 2 and the other for example in frame 40. What
we're going to do is make the timeline play to the last
keyframe and then loop back to frame 2 where it refreshes
itself. The interval between the refreshes depends on your
fps of the movie and the length between keyframe 2 and 3
in frames. It's wise set the interval more than 4 seconds
since it has to load the text file all over again every
time it refreshes itself. But it isn't a big deal because
the text file is just over a few bytes.
[ keyframes
in frames 1, 2 and 40]
-
Now to make it work! In frame 2 copy this
action:
this.loadVariables("count.txt?num="+random(999));
This loads the
text file named "count.txt" every time it loops back to
that frame Notice the "?num="+random(999)"
part. That's a query
string. More about query strings in step VI.
-
Now in keyframe 3 (frame 40) copy these
actions:
gotoAndPlay(2);
This loops the playhead
back to frame 2 (and refreshes itself).
-
In frame 1 copy this action:
this.loadVariables("counter.php?num="+random(99));
This executes the php file that we will be
writing soon. This php file will increase the number of
visitors as well as return the number of visitors (after
increasing). And again we use a query string.
That's where we use a query
string to make sure that it doesn't automatically load an
old php file from the browser's cache. we change the
variable's value to a random number between 0 and 99 so
the user has a 1 out of 99 chance of getting an old text
file from the cache. This time the
variety of the random number doesn't have to be so large
since we don't change the php file anyway (actually you
can just leave it out).
Now all you need to do is publish the
movie and we'll start writing the php file. But before that
we must create the text file.
Creating the Text
File
Open up a text editor.
Type in "count=0" (without double-quotes) and save it as "count.txt".
Creating the PHP File
-
Open up your favorite text editor. Copy and paste the
following code:
-
PS! Now if you have an older version of php than 4.3.0
replace
$count =
file_get_contents("count.txt");
with
$file = fopen("count.txt",
"r");
$count = fread($file, 10240);
fclose($file);
This does exactly the same thing but in a more complicated
way :P
Code explanation
Now I will simply explain the code...
$count =
file_get_contents("count.txt");
This little piece of code
simply "gets" the contents of "count.txt" and assigns it to
a variable (in this case $count) as a value. [ This is the
4.3.0 version of it ]
$file = fopen("count.txt", "r");
$count = fread($file, 10240);
fclose($file);
This does the exact same
thing but in a more complicated way. It first opens up the
file for reading. (different opening modes
here)
Then it "reads" the file.
That 10240 there is just the maximum length in bytes that it
will read. (I'm sure your counter will never get to that
point ;) )
And the third line just closes the file.
$count = explode("=",
$count);
$count[1] = $count[1]+1;
The first line splits the
data from the text file into an array named $count (the same
as the variable before) (more about arrays
here). So that the first element in the array is just
the word "count" and the second is the exact count of
visitors (right now it's "0").
Since the elements in an array are counted starting from 0
(0, 1, 2, etc) we take the first element (actually the
second one numbered "1") and increase the value of that
number.
$file = fopen("count.txt",
"w+");
fwrite($file, "count=".$count[1]);
fclose($file);
The first line opens up the file for
writing and truncates its length to 0.
(different opening modes
here).
The second line writes the new data into the text file. in
the form of
"count=" + the number.
The third line simply closes the file.
print
"count=".$count[1];
This returns the count in the form of
"count=" + the number. This will automatically change the
value of the textbox we created in flash because the php
file is loaded into flash and the php script will give a
command to change the value of "count" to the number of
visitors. (count was also the variable-name of our textbox
remember?)
Now probably the most important thing you
have to do now is when you upload your files onto a server
make sure you change the permissions of the text file to
777. This can be done with telnet, Smartftp, etc. I use
Smartftp. (it's most probably found under the properties of
the text file)
And you're done! Your counter should be up
and running! Use the search feature at
php.net to find out what
exactly each function does and how it's used.
If you have any questions feel free to post them in the
forum.