HIT COUNTER FOR FLASH USING TXT FILE as3,php |
Very simple hit counter tutorial for your web using basic php and txt file to store the information.
This counter does count every page load, meaning it does not ahve unique visitor filter built in. At the end we should get something like this :
All we need is dynamic text field with instance name output1_txt. Rest of this tutorial is pure actionscript nad php. Select frame one and enter
following actionscrip:
var urlLoader:URLLoader = new URLLoader( ); urlLoader.addEventListener( IOErrorEvent.IO_ERROR, handleIOError ); urlLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, handleHttpStatus ); urlLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, handleSecurityError ); urlLoader.addEventListener( Event.COMPLETE, handleComplete ); urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES; urlLoader.load( new URLRequest( "dataFile.txt" ) ); function handleIOError( event:IOErrorEvent ):void{ trace( "IO error: " + event.text ); } function handleHttpStatus( event:HTTPStatusEvent ):void{ trace( "HTTP Status = " + event.status ); } function handleSecurityError( event:SecurityErrorEvent ):void{ trace( "Security Error: " + event.text ); } function handleComplete( event:Event ):void{ trace( "Data loaded" ); trace( "name1 :: " + urlLoader.data.name1 ); trace( "name2 :: " + urlLoader.data.name2 ); output1_txt.text = urlLoader.data.name1; }
Line one creates new loader variable. Lines 3-6 adds event listeners to IO errors, HTTP status and security errors. In case somethign goes wrong
this will help to identify the source of problem. Each of this listeners calls their respective functions defined below them. Line 10 load variables from txt file. Make sure
the path is correct. Function between lines 22-27 executes after all variables have been loaded successfully and, in this case, send variable number one to the text field.
This tutorial works with two stored variables in case you would like to store another information, such like IP, date, browser type etc. Ofcourse this information has to be
written to the txt file using php first. All you need to do is to replace the value of second variable in php file with your desired information. The php code itself is below.
This php code is using two txt files, first ot read and increment from for php script itself, and second for flash to read from, since flash requires
certain symbols to recognize every variable stored in file.
|