Link click count in php

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

Right now, what I'm doing is linking href to redir.php, which will increment the counter in the DB, and then with the help of header('Location:');I redirect it to the correct URL.

It works, but it is certainly not the best approach. To make my code efficient, how can I make this link count better? AJAX?

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

I do not want someone to write a bot script that would make a few requests to redir.php and ruin the statistics.

+3
source share
5 answers

you can use

  • Javascript Ajax "counter.php"
  • Javascript- (, Google Analytics)
  • " cron" "access_log" ( , )
  • PHP- .

, javascript .

  • spy
  • " " AJAX
  • PHP script .

+2

, CMS, , URL- , . ( ) , , -, , .

, javascript, javascript, , AJAX.

, ( , AJAX), , javascript , ,

+1

@Akarun, ( jQuery) "" "" . , , $.post AJAX - , , ( ), . - PHP , , .

$(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;
    });
});
+1

, Google:

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

javascript. .

Actually, looking at a Google source, they upload an image with a 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
};
0
source

Have you thought about mobile users and other devices? I believe that your first implementation is completely adequate and safe. You have complete control over the count, and there are no problems with manipulating the user. It also works predictably.

In the end, ajax will just do something in counter.php; Read and update the database. Stay with your current implementation.

0
source

All Articles