1

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

这是我的代码:

<?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>

我的猜测是它必须与 header('Location: ' . $_SERVER['SCRIPT_NAME']); 但我无法弄清楚,所以我真的可以使用一些帮助。

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

提前致谢!干杯!

4

3 回答 3

2

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

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

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

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

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而不是文本文件

于 2014-09-28T17:04:42.453 回答
1

您可以使用 Javascript 捕捉链接点击,通过 AJAX 调用发送数据。这是使用 JQuery 的小示例。

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script>
            $(
                    function() {
                        $('a').click(linkClicked);
                    }
            );
            //this funciton will be called on every click on any link on page
            function linkClicked() {
                var url = $(this).attr('href');
                //call PHP script to save URL ./saveurlclicks.php?url=CLICKEDURL
                $.get('./saveurlclicks.php', {'url': url})
                //be sure to return true so user can navigate further
                return true;
            }
        </script>
    </head>
    <body>
        <a href='/some href' >asasa</a>

        <a href="www.google.com" >google</a>
    </body>
</html>

<?php
//saveurlclicks.php
// here we save links in file but using serialized array
// if you need to get count of links clicked , 
// have a second script that unserializes array and sort it in revers order 
$url = @$_GET['url'];
$counterFile = 'counter.ser';
if ($url) {
    if(file_exist($filename))
    $links = unserialize(file_get_contents($filename));
    else $links=array();

    if (!isset($links[$url])) {
        $links[$url] = 0;
    }
    $links[$url] ++;
    file_put_contents($counterFile, serialize($links));
}
于 2014-09-28T17:35:42.973 回答
0

我喜欢 Paul S. 的简单解决方案,但如果您想使用日期跟踪点击次数,可以执行以下操作:

2022 年 3 月 3 日 14
2022 年 4 月 3 日 2

    <?php
$dateexists = false;

if(!file_exists('counter.txt'))
    { $fh = fopen('counter.txt', 'w');
      fclose($fh); }

$datecounts = file('counter.txt', FILE_IGNORE_NEW_LINES);
foreach($datecounts as $key => $datecount){

list($date, $count) = explode("\t", $datecount);

$count = (int) $count;
if($date == date('d/m/Y'))
{   $datecounts[$key] = $date."\t".++$count;
    $dateexists = true; }
}
if(!$dateexists)
    { $datecounts[] = date('d/m/Y')."\t1"; }

$fh = fopen('counter.txt', 'w');
if (flock($fh, LOCK_EX)) {
    foreach($datecounts as $datecount)
    { fwrite($fh, $datecount.PHP_EOL); }
flock($fh, LOCK_UN);
}

else
{ //couldn't lock, might want to do stuff here }

fclose($fh);
header('Location: ' . $_GET['href']); // the redirect
?>
于 2022-03-04T19:33:07.157 回答