计算器中链接的点击次数

时间:2023-02-03 03:52:47

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.

现在我正在做的是,我将href链接到redir.php,这将增加DB中的计数器然后使用header('Location:');我正在将其重定向到正确的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?

这有效,但肯定不是最好的方法。为了使我的代码高效,我怎样才能使这个链接计数器变得更好? AJAX?

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

使用ajax的exp不多,所以我想知道如何在ajax中做什么或者还有其他更好的方法......

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

我不希望有人写一个机器人脚本,它会向redir.php发出多个请求并弄乱统计数据。

5 个解决方案

#1


2  

You can use

您可以使用

  1. Javascript to make a Ajax call to your "counter.php"
  2. Javascript对您的“counter.php”进行Ajax调用

  3. Add a Javascript code (like Google Analytic) on each page to post on the database
  4. 在每个页面上添加Javascript代码(如Google Analytic)以在数据库上发布

  5. Create a "cron job" to analyse the "access_log" (if you count the link in the same domain, server)
  6. 创建一个“cron作业”来分析“access_log”(如果计算同一域中的链接,服务器)

  7. Add a PHP code to update the database when each page is generate.
  8. 添加PHP代码以在生成每个页面时更新数据库。

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

但我认为第一个javascript方法是最好的方法。

  1. Add a class on the link to spy
  2. 在链接上添加一个类给间谍

  3. Add a "Event handler" to create a AJAX post
  4. 添加“事件处理程序”以创建AJAX帖子

  5. Create a simple PHP script to update the database.
  6. 创建一个简单的PHP脚本来更新数据库。

Aka

#2


1  

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.

如果链接是从CMS等源而不是手动生成的,则可以将链接ID传递给URL,并加载链接已被单击的下一页计数增量。这样做需要你在该步骤之后重新加载(没有链接ID)页面,以确保复制链接的人不会不必要地使计数器增加。

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.

如果您的用户启用了javascript,此方法是防弹的,但如果您的用户确实启用了javascript,您仍然可以执行上述方法并通过客户端层,绕过整个过程并通过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

这可能看起来像冗余,但是这样,你加快了大多数访问者的流程(没有重定向,因为你通过AJAX这样做),如果javascript不起作用或被禁用,你有一个失败的证据系统可避免遗漏任何点击

#3


1  

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.

基于@Akarun的答案,这里是一个示例代码(在jQuery中),用于在“spy”类的链接点击上添加“监听器”。请注意,我加载了一个图像,而不是尝试$ .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;
    });
});

#4


0  

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.

毕竟,ajax会在counter.php中做同样的事情;读取并更新数据库。坚持目前的实施。

#5


0  

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.

一个javascript函数。传递的代码有助于安全性。

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

实际上,在查看Google源代码时,他们会将URL作为参数加载图像

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
};

#1


2  

You can use

您可以使用

  1. Javascript to make a Ajax call to your "counter.php"
  2. Javascript对您的“counter.php”进行Ajax调用

  3. Add a Javascript code (like Google Analytic) on each page to post on the database
  4. 在每个页面上添加Javascript代码(如Google Analytic)以在数据库上发布

  5. Create a "cron job" to analyse the "access_log" (if you count the link in the same domain, server)
  6. 创建一个“cron作业”来分析“access_log”(如果计算同一域中的链接,服务器)

  7. Add a PHP code to update the database when each page is generate.
  8. 添加PHP代码以在生成每个页面时更新数据库。

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

但我认为第一个javascript方法是最好的方法。

  1. Add a class on the link to spy
  2. 在链接上添加一个类给间谍

  3. Add a "Event handler" to create a AJAX post
  4. 添加“事件处理程序”以创建AJAX帖子

  5. Create a simple PHP script to update the database.
  6. 创建一个简单的PHP脚本来更新数据库。

Aka

#2


1  

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.

如果链接是从CMS等源而不是手动生成的,则可以将链接ID传递给URL,并加载链接已被单击的下一页计数增量。这样做需要你在该步骤之后重新加载(没有链接ID)页面,以确保复制链接的人不会不必要地使计数器增加。

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.

如果您的用户启用了javascript,此方法是防弹的,但如果您的用户确实启用了javascript,您仍然可以执行上述方法并通过客户端层,绕过整个过程并通过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

这可能看起来像冗余,但是这样,你加快了大多数访问者的流程(没有重定向,因为你通过AJAX这样做),如果javascript不起作用或被禁用,你有一个失败的证据系统可避免遗漏任何点击

#3


1  

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.

基于@Akarun的答案,这里是一个示例代码(在jQuery中),用于在“spy”类的链接点击上添加“监听器”。请注意,我加载了一个图像,而不是尝试$ .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;
    });
});

#4


0  

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.

毕竟,ajax会在counter.php中做同样的事情;读取并更新数据库。坚持目前的实施。

#5


0  

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.

一个javascript函数。传递的代码有助于安全性。

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

实际上,在查看Google源代码时,他们会将URL作为参数加载图像

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
};