New to Flash? Come join Ben in this, the first of many weekly Flash articles, as he shows us how to use Flash and PHP to show a count of users on your web site.
Dynamic Flash Part 1: Counting Users Online - Whose Online Using Flash (contd.) (Page 4 of 5 )
Ok, now that we have our dynamic file we can create our flash movie to display the number of users online, without refreshing the page. We will manipulate our Flash movie to refresh every 20 seconds, which will call up the count.php file again, thus getting a refreshed value for the number of users online.
Create a new flash movie called 'count.fla' with dimensions of 200x80. When we’re done you will need to upload it to your web server.
Create some text called 'Active Users:'. Create another text field and change its properties to dynamic text. Call the var 'Count':
Ok, so now we have two text fields: one static, and one dynamic. Up in the timeline properties, select frame 1:
This will load the variable contents of the count.php file and tell us how many users are online. Remember that we append the random number so that we don't receive Flash's cached version of count.php. Our movie is nearly complete and will display the number of users online, however it doesn't yet display that number on the fly. To do this we need to create a loop using the timeline. We want it to reload the count.php file every 20 seconds. Bbecause there are 12 frames per second, we need to add a command to frame 240.
Firstly, select frame 240 and hit F6. This will create a key frame so that we can add a new action. Now that we have our key frame created, make sure it's selected and add the following to the action properties:
gotoAndPlay(1);
This will tell the movie to loop, causing it to check the PHP file again. Save your work and hit Ctrl+F12. Close the browser.
Goto the directory where you saved your movie to and grab the following files: count.php, count.html, count.swf. Upload them to your server and then run count.html in your. It should display as Active Users: 1.
Now, jump to another computer or ask a friend to visit your site and load up the count.php file in their browser. Do not refresh the page -- It will defeat the purpose of using flash.
Once your friend has loaded the page, wait 20 seconds and the flash movie should automatically update, showing you the number of people on your site!
Follow the link below to see an example flash movie showing how many people are currently browsing the DevArticles web site.