单击链接时点击计数器PHP / JS [英] Click counter when link is clicked PHP/JS

查看:94
本文介绍了单击链接时点击计数器PHP / JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个小脚本,可以在点击链接时对点击进行计数并将其存储在.txt文件中,但是当我在href下只有click = yes时它可以正常工作。但是当我链接到外部网站时,我无法跟踪点击次数。

I have a little script here that counts clicks when link is clicked and stores it in .txt file, but it works fine when I have only "click=yes" under href. But I can't make it to track clicks when I have link to external site.

这是我的代码:

<?php
if(!file_exists('counter.txt')){
file_put_contents('counter.txt', '0');
}
if($_GET['click'] == 'yes'){
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>counter example</title>
</head>
<body>
<h1><?php echo file_get_contents('counter.txt'); ?></h1>
<a href="http://www.google.com?click=yes" target="new">clickMe</a>
</body>
</html>

我的猜测是必须对标题执行操作('位置:'。$ _SERVER ['SCRIPT_NAME' ]);但是我无法理解,所以我真的可以使用一些帮助。

My guess is it has to do something with header('Location: ' . $_SERVER['SCRIPT_NAME']); but I can't figure it out so I could really use some help.

以某种方式可以将多个链接保存到同一个文件中,当我显示时它在网站上从最大数量到最小数量排序?我知道如何使用MySQL数据库,但我不能在将要实现的地方使用它。

And is it somehow possible to have multiple links save to the same file, and when I show it on website it's sorted from largest number to smallest? I have an idea how to do it with MySQL database but I can't use it at place where this will be implemented.

提前致谢!
干杯!

Thanks in advance! Cheers!

推荐答案

当客户端离开您的页面时,您的服务器永远不会看到 URI 被访问。要做这样的事情,最好设置一个像这样工作的重定向

Your server never sees the URI being accessed as the client leaves your page. To do something like this, it may be best to set up a redirect which works like this

<a href="/goto.php?href=http://www.google.com" target="_blank">click me</a>

(确保外部网站的网址 URL编码,因为你是将它作为 URL 的GET组件传递到您自己的页面)

(Make sure the external site's URL is URL encoded as you're passing it as a GET component of a URL to your own page)

然后在 goto.php 存储点击并发送重定向标题

Then in goto.php you store your click and send a redirect header

if(!file_exists('counter.txt')){
    file_put_contents('counter.txt', '0');
}
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header('Location: ' . $_GET['href']);






现在你可以跟踪这些点击,你可以添加您在 goto.php 中的特定于域的计数器,而不是您的文本文件


Now you can track these clicks, you can add your domain-specific counters in goto.php instead of your text file

这篇关于单击链接时点击计数器PHP / JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆