AJAX Click Counter & Statistics

So a while ago I needed a custom click counter, to keep track of clicks and statistics for clients. As usual I googled first, just to realise there are some really crappy counters out there. As a result, I decided to make one myself using AJAX and it has been working flawlessly after some touches here and there.

I decided to load a php page, located somewhere else, which keeps track of the clicks through some basic GETs, no rocket science here.

if (isset($_GET['click']))
{
	$link_name = $_GET['click'];
	$uid = $_GET['uid'];

	//includes databases settings and the variable $table, which is the name of your mysql table
	include('settings.php');

	$createTable = "
			CREATE TABLE IF NOT EXISTS $table (
			  `id` int(11) NOT NULL auto_increment,
			  `name` text NOT NULL,
			  `ip` text NOT NULL,
			  `uid` text NOT NULL,
			  PRIMARY KEY  (`id`)
			) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0
			";

	mysql_query($createTable) or die(mysql_error());

	$link_name = mysql_real_escape_string($link_name);
	$uid = mysql_real_escape_string($uid);
	$ip = getenv('REMOTE_ADDR');

	$query = "INSERT INTO $table (name, ip, uid) VALUES ('$link_name', '$ip', '$uid')";

	mysql_query($query) or die(mysql_error());

	exit("1");
} else { echo "Not a valid click"; }

Now to make some javascript that actually loads the page. I put the php code in ‘stats/stats.php’.


var req = false;
function loadXMLDoc(dname, func) {
	if (window.XMLHttpRequest) {
		xhttp=new XMLHttpRequest();
	}
	else {
		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhttp.onreadystatechange=function() {
		if (xhttp.readyState==4 && xhttp.status==200) {
			func.call();
		}
	}
	xhttp.open("GET",dname,false);
	xhttp.send();
	return xhttp.responseXML;
}

function gotoUrl(url) {
	window.parent.location = url;
}

function countClick(name, uid, gotourl) {
	url = 'stats/stats.php?click=' + name + '&uid=' + uid;
	func = gotoUrl(gotourl);
	//alert(url);
	loadXMLDoc(url, func);
}

Pretty basic, just call the function countClick when clicking on something in the HTML and you’re off to go. Be sure to give each clickable object an unique name. I added in the ‘uid’ identifier for own convenience, I use it to keep track of the clicks of all separate users.

I even added in a ‘countClickFunc’ for convenience, in case I need to call a function instead of redirecting to an url. Here’s an example of the HTML code:

<a onclick="countClick('testbutton', '1', 'http://www.exasm.net')">testbutton</a>

Now I know this is not perfect, but bear with me that these are just the basics, if you don’t trust your users, make sure to put some kind of anti-spam on the php script for example.

First Milestone of project Exasm

Aight, welcome folks. I’m here to announce… it has begun. That’s right, the most awesome project of the century has started: project Exasm (sorry for the lack of a… cool name).

I don’t think I’ll be giving away too much info, for now, as I kind of want it to be a personal learning experience, rather than an amazing project. It’s main goal is to notch up my C++ skills a bit. For the time being, I’ll tell you the project will surely involve:

  • A global server;
  • ‘Dedicated’ client servers;
  • A real time connection;
  • Awesome graphics;

I know it’s not much, so please try using your imagination for the time being:).

Anyhow, I decided to start on the main client today, which will basically become a connection to the global server at some point. I managed to get up some random GUI using the Qt lib, which is amazing by the way. I even managed to cramp in a random button event, displaying a printf().

And that’s pretty much it for now, I’ll be getting started on the first code of the server tomorrow. Stay tuned!

Hello derp!


Yes indeed, another ‘hello derp‘ post.

First of all, welcome! If you’re looking at this blog somewhere in March 2012…. sorry for the fugly layout and everything. Just felt like getting this first blog done, I’ll fix the rest later.

So to start out, a little introduction and as to why the hell this website even exists. I’m a CE (Computer Engineering) 19-year-old (as of writing) student in the Netherlands and software and webdeveloper at Nubis Online. As you can guess, I have profound interest in programming and everything that even has the slightest connection to it.

So as we speak, I just finished programming a Facebook application and just this week I came to the conclusion that I should broaden my knowledge. And I reckon the best way to do this is to just get started. The main point of this blog will be about my journey as a C++ programmer and learning how to handle linux (servers).

I started out this week by installing Linux Mint and I’m honestly having a hard time going around in Linux and working with the terminal, I refuse to use the GUI, except for googling on how to not use the GUI. However, I managed to get around and compile some ‘Hello World!’ code.

Grasping the C++ syntax won’t be too much of a problem, I think. I’ve got enough experience with Java, C, C# and ofcourse PHP for my webdevelopment projects. So, my main problem right now, is actually getting used to linux. I plan on making a simple user registration system with a GUI, and perhaps a chat room of some sort. I’ve done some research and I’ll probably use the Qt library for this.