This tutorial will explain how to make a simple yet effective access counter for your page with Flash Communication Server.

Download sources

You're probably wondering why you'd make something like this with FCS.  It of course can be done easily with cgi, asp, or cfm but that requires the knowledge of server  side languages, and you'd have to combine the output of that with Flash (if you want to show it inside flash).  This works straight from flash, and shouldn't put any strain on your server, even if you only have a developer package running. Another good thing is that there is no need for a database, which makes this even easier to implement. I'll explain why in this tutorial.

To hold the count for the amount of times the page is accessed we will be using a server side shared object.  There are two types of server side shared objects, persistant ones and non-persistant. This just means that when there is nobody connected to the shared object, if it is persistant the data will be saved, if not then  the SO get's deleted.  We will be using a persistant shared object for the access counter because we don't want the data to disappear just because nobody is connected.

Up above you should see a small counter movie created in Flash.  This is nothing special, just a grey rectangle with a dynamic textbox in the middle.  Let's see how I put this together.

The first thing we are going to work on is the Server Side actionscript.  If you have only used communication components up until now, this will be good practice for you  I think. It's not so difficult so lets go through this line by line.

The first line we have here is the application.onAppStart() function.  This gets run everytime the application starts up. What we are going to do here is get a hold of  the shared object and find out the count value that is in there.  The count value will be how many times the page (or wherever you put the movie) has been looked at.

To get the shared object we use SharedObject.get(). Inside there we put the name of the shared object (which is the file name) and whether it is persistant or not.   Remember to be careful with making it persistant or not as they are two different objects and you won't be able to access a persistant one if you put "false" in the persistant parameter spot.  True is persistant and false is non-persistant

For example SharedObject.get("counter",true); and SharedObject.get("counter",false); are very different.

In our case we are placing the SO in the variable counterSO. Then we place the value of the count in a variable called tempCount and check if it's undefined.  If it is,  then this is the first time that this application has been run, and we should set the default value of 0.   To get a value of a variable in an SO we will use  getProperty(propertyName);  To set a value in an SO on the server side, you will use setProperty(variableName,value);  So in our case we want to set the counterSO's  count value to 0. So we would write counterSO.setProperty("count",0); Keep in mind even when setting values from the server side, all clients connected to this SO will receive the onSync call that the values inside the SO have changed. We won't cover that topic here though.

We are also going to set a variable called lastIP to nothing.  This will be used later to test whether it is the same user pressing reload or coming back really soon  from jumping somewhere else. We wouldn't want the count going up just because somebody kept coming back to this page or is having fun and pressing reload a couple of hundred times (which I have actually done to somebody... :) ) Not a cool counter in my mind.

The next application method we are going to set is the onConnect() function.  This gets called whenever a client connects up to our application.  We are going to need  the client object (which holds all kinds of info!) so pass that into the function by placing a word (I use clientObj, but any word is fine really) in the parantheses.   So we will write application.onConnect=function(clientObj).  Inside this function is all the code that makes this access counter work. 

The first thing we want to do is accept the connection.  I strongly suggest using a bit of security like finding out where the client is coming from and comparing it to  your own domain, but that is covered in another tutorial, so I won't add it in here.

Next get the client's IP number.  This is retrieved by using the client's object that we passed in, "clientObj", and getting the property IP. So we will write nextIP=clientObj.IP;
This places it in the variable nextIP which we will now compare it to the "lastIP".  If they are not the same (which means a different user came in  than the last one) then add one to the counter.  Adding one is simple enough as we are just going to get the count value of the SO and add 1 to it.  Then we place the  users IP number in the lastIP variable and make a call to the user to set the counter to the current count.  If this person is the last person who connected up then we  won't count them and just get the current count and send it back to them.

To call a function sitting on the client side from the server side, we will use the client method call().  We want to call the function (which we will write later)  "setCounter" on the client and pass in the number of times the page has been accessed.  To do this we will write:"setCounter",null,nextCount);  We have  placed a null in the resultObject parameter because we don't expect anything to come back.  Keep in mind, when you don't expect anything back, you have to set it to null  or the server will throw an error saying it couldn't find the function mainly because we are passing in nextCount which would be seen as the resultObject callback  function.  If you don't put in any variables to pass across then the null is not needed.

OK, so save this file as main.asc in your application directory on the server and now we'll move on to the client side.

This is extremely simple I think, as there are only 8 lines to enter.  Make sure you have placed a dynamic textbox on the stage called counter_txt (or whatever you want  to call it, just make sure it matched up with the AS).

The first thing we are going to do is create the object that will hold the NetConnection object.  It's a good idea to use one name for all your apps, you never know when  you might want to copy and paste some code around and it's a pain to keep changing the name of the netConnection object you are using (of course there are some apps that  will need more than one name but...)  We are going to call ours, "conn_nc".  Putting the "_nc" on the end will enable netConnection popup code hints which is nice so I  suggest adding it in.

Another good practice is to set your URL in one place, and even better would be to split up your URL into server name and application name and instance name.  But since  this is a simple tutorial we'll put it all in one variable called URL.

The next thing is to define what should happen when we connect up, but since this is only a measly counter we won't do that.  I'll cover that in another tutorial.

The next thing would be to define the setCounter function that is going to get called from the server after the client is allowed to connect up.  This will accept one  variable, "count" the access count, and place that number in the text box. 

To setup a function that is going to get called from the server make sure to extend it off of the netConnection object.  In our case here it is called conn_nc so we will  write: conn_nc.setCounter=function(count) .  After we have set the text box we don't need to be connected up to the server anymore so we'll close it and delete the  object to save a bit of memory. 

Since we have only connected to the server for what would seem less than a second, there is a very good chance that even if you had a lot of users popping in, you  wouldn't go over a 5 or 10 connection max limit (on a developer account maybe).

How can we make this even better? Well we could completely take out the chance that the same user won't be connected and save a local shared object saying that  connectedOnce=true or something like that.  Then have the app compare that first before connecting. If it is true then pass that true value in, so the server doesn't have to compare IP numbers and just passes back the access count.

On a security level, you could even setup a function that will disconnect the user if they have been connected up for more than 2 or 3 seconds.  This is just in case  your domain checking function has been bypassed somehow.

That concludes this tutorial on making a quick access counter with FCS.