3
votes

I need to implement a links-click counter, that will count the number of clicks on the link...

Right now what i am doing is, i am linking the href to redir.php, which will increase the counter in DB and then using header('Location:'); I am redirecting it to the correct URL.

This works but it is certainly not the best approach. In an effort to make my code efficient, how can I make this link counter better? AJAX?

Not much exp with ajax so I wondering how to do in ajax or is there any other better method...

I do not want someone to write a bot script that would make multiple requests to the redir.php and mess up the stats.

5

5 Answers

2
votes

You can use

  1. Javascript to make a Ajax call to your "counter.php"
  2. Add a Javascript code (like Google Analytic) on each page to post on the database
  3. Create a "cron job" to analyse the "access_log" (if you count the link in the same domain, server)
  4. Add a PHP code to update the database when each page is generate.

But I think the first javascript method is the best one.

  1. Add a class on the link to spy
  2. Add a "Event handler" to create a AJAX post
  3. Create a simple PHP script to update the database.

Aka

1
votes

If you links are generated from a source like a CMS instead of by hand, you could pass the link ID to your URL and on the loading of the next page count increment that the link has been clicked. Going this way would require that you reload (without the link ID) the page after that step to make sure that someone copying the link would not make the counter increment needlessly.

This method is bulletproof if your user has javascript enabled, but if your user does have javascript enabled, you could still do the method stated above and through a client side layer, bypass the whole thing and send it through AJAX.

This might seems like redundancy, but this way, you accelerate your process for most of your visitors (without the redirect since you do it through AJAX) and in the case that the javascript doesn't work or is disabled, you have a fail proof system that would avoid missing any click

1
votes

Building off of @Akarun's answer, here is sample code (in jQuery) for adding a "listener" onto link clicks with "spy" class. Note that I load an image instead of attempting a $.post or other AJAX event -- this is because those won't complete by the time the person navigates away from the page (which clicking on a link is bound to do in most cases), whereas the browser will get off a request for the image in time. It's still a normal PHP script, the browser just thinks it's loading an image.

$(document).ready(function() {
    $('a.spy').mousedown(function(event) {
        var page_url = "<?=$_SERVER['PHP_SELF']?>";
        var target_url = $(this).attr('href');
        if(target_url != "#" && target_url != "javascript:void(0);")
            new Image().src= "/welcome/track_link/?page_url=" + escape(page_url) + "&target_url=" + escape(target_url);
        return true;
    });
});
0
votes

Have you thought of mobile users and other devices? I believe your first implementation is completely adequate and secure. You completely control the counting and there is no issue of user manipulation. It works predictably also.

After all, the ajax will just do the samething in counter.php; Read and update the database. Stay with your present implementation.

0
votes

Do it the way Google does it:

<a href="http://www.vacationhomes.com/" onmousedown="return clk(this.href,'','','','2','','0CE4Q0gIoAzAB')">Waterfront Rentals</a>

A javascript function. The passed code aids security.

Actually looking at the Google source they load an image with the URL as a parameter

window.clk=function(e,b,a,k,i,c,j)
{
    if(document.images) {
        b=encodeURIComponent||escape;a=new Image;var f=window.google.cri++;window.google.crm[f]=a;a.onerror=a.onload=a.onabort=function() {
            delete window.google.crm[f]
        };
        var d,g,h;if(google.v6) {
            d=google.v6.src;g=google.v6.complete||google.v6s?2:1;h=(new Date).getTime()-google.v6t;delete google.v6
        }if(c&&c.substring(0,6)!="&sig2=")c="&sig2="+c;a.src=["/url?sa=T&source=",google.sn,"&cd=",b(i),google.j&&google.j.pf?"&sqi=2":"","&ved=",b(j),e?"&url="+b(e.replace(/#.*/,
            "")).replace(/\+/g,"%2B"):"","&ei=",google.kEI,d?"&v6u="+b(d)+"&v6s="+g+"&v6t="+h:"",c].join("")
    }
    return true
};