PHP-MySQL Site counter

Charts is courtesy of maani.us.

Site Counter for all and unique visitors

Introduction

This counter allows to count site visitors in two ways either unique visitors who are those who come back but are not counted again. Or whenever the site is called there will be a count.

Flash Actionscript

The Flash AS is quite simple. All we do is have textfields for labels and the counts. Then we call a php file and wait to get data back. Those are the data for the textfield counter. We use the URLLoader object. It is important even we do not send any variables that we define the "dataFormat", because we are getting data in form of variables back from the php file. One important point is that we set the cookie after increasing the value for the visitors if we want to count unique visitors. Please check the SiteCounter.as file for the whole script how to create this component. I have set the cookie to expire after one day.



cClip.textLabel.text = tLabel;
var request:URLRequest=new URLRequest();
request.url = receiveData;
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader  ;
if (!allVisits)
{
	var myVariables:URLVariables = new URLVariables();
	myVariables.unique = false;
	request.data = myVariables;
}
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load (request);
loader.addEventListener (Event.COMPLETE, received);
/*
* The received function is triggered when data are coming back from the 
* server. We create an XML file using the node from the server and get
* the data.
*/
function received (event:Event):void
{
	var loader:URLLoader = event.currentTarget as URLLoader;
	var myData:XML = new XML("" + loader.data.myResult + "");
	loader.removeEventListener (Event.COMPLETE,received);
	allCounts = int(myData.alcounts);
	cClip.countVisits.text = String(allCounts);
/*
* We send the message to the html page now to set a cookie. It has to be
* done after incrementing the value.
*/
	if (! allVisits)
	{
		var myCookie:String = String(ExternalInterface.call("setOneCookie",myHTML));
	}
}
	
	
		

Javascript for cookies

If we want unique visitors we need to mark the users harddrive either using a cookie or the Sharedobject. I prefer to do most of the manipulations outside of Flash, so I use a cookie. In this case I am using Javascript to set the cookie. This script is from the W3C Schools. It is very simple to implement into the header of your html page. we give it a name and add a value and add the number of days (here 1).



<script type="text/javascript">	
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
setCookie("name","value",1);
</script>
	
		

next