This tutorial shows how to create a visitor counter in Flash.
The fla file
Make a movieclip with a textbox. Then on the main timeline create an actions frame and put the script below inside. First we define some variables to hold the counts and LoadVars objects.
stop(); //defining variables import mx.utils.Delegate; var downLoads:Number; var newDown:Number; var numLoaded:TextField; numLoaded.text = "000000"; var rememLoads:Number; // var lv:LoadVars; lv = new LoadVars (); var lvAnswer:LoadVars; lvAnswer = new LoadVars ();
Then we load the xml file with the counts. We do that by using an interval to give enough time to load. All those lines which you see are just to make the count appearance nicer.
// var fileCount:String = "site_counter.xml"; //this is to bypass the xml cache if (System.capabilities.playerType != "External" && System.capabilities.playerType != "Standalone") { fileCount += "?"+new Date().getTime()+Math.floor(Math.random()*10000); } //loading xml to check for the number of visitors var myCounts:XML = new XML(); myCounts.ignoreWhite = true; myCounts.onLoad = function() { //we make the textnodes numbers here and load them into invisible textfields downLoads = parseInt (this.firstChild.firstChild.firstChild); var timer:Number = setInterval (Delegate.create (this, counter), 500); function counter () { if (downLoads != undefined) { clearInterval (timer); if (downLoads<10) { numLoaded.text = "00000"+downLoads.toString (); } if (downLoads<100 && downLoads>10) { numLoaded.text = "0000"+downLoads.toString (); } if (downLoads<1000 && downLoads>100) { numLoaded.text = "000"+downLoads.toString (); } if (downLoads<10000 && downLoads>1000) { numLoaded.text = "00"+downLoads.toString (); } if (downLoads<100000 && downLoads>10000) { numLoaded.text = "0"+downLoads.toString (); } if (downLoads>100000) { numLoaded.text = downLoads.toString (); }
At this point now we send data to the php file. We create this if statement so that only numbers are sent.
if (downLoads != undefined || downLoads != "NaN") {
newDown = downLoads+1;
lv.newDown = newDown;
lv.action = "count";
lv.sendAndLoad ("site_counter.php", lvAnswer, "POST");
Now we wait what comes back from the php file and when all is ok we show the new number.
lvAnswer.onLoad = function () {
if (this.myCount == "Okay") {
downLoads = newDown;
if (downLoads<10) {
numLoaded.text = "00000"+downLoads.toString ();
}
if (downLoads<100 && downLoads>10) {
numLoaded.text = "0000"+downLoads.toString ();
}
if (downLoads<1000 && downLoads>100) {
numLoaded.text = "000"+downLoads.toString ();
}
if (downLoads<10000 && downLoads>1000) {
numLoaded.text = "00"+downLoads.toString ();
}
if (downLoads<100000 && downLoads>10000) {
numLoaded.text = "0"+downLoads.toString ();
}
if (downLoads>100000) {
numLoaded.text = downLoads.toString ();
}
}
};
}
}
}
};
myCounts.load (fileCount);
The PHP file
Here we first make sure the incremented number of visitors is defined. Then we create a variable holding new xml with the updated number. We have a switch function and then go to a function where we rewrite the new xml. Then we exit and send the result back to flash, very simple.
<?PHP //testing if var from flash are defined and have contents if (!isset($newDown)) { fail(); } $counted = "<counter><count>$newDown</count></counter>"; //function to add or delete information switch($action) { case "count": countnow($counted); break; default: fail(); } //function to add information function countnow($counted) { $filename = "site_counter.xml"; $file = fopen($filename, 'w'); if (!$file) { fail("Error: Couldn't open xml file"); } //writing the replaced information fwrite($file, "$counted"); fclose($file); success(); } //function in case of failure function fail() { print "&myCount=Fail"; exit; } //when it was successful... function success() { print "&myCount=Okay"; exit; } ?>
Now finally the xml file. We set the count to 1 and now you are ready to upload and test the counter.
<counter><count>1</count></counter>