简体   繁体   English

单击链接时单击计数器 PHP/JS

[英]Click counter when link is clicked PHP/JS

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.我这里有一个小脚本,可以在单击链接时计算点击次数并将其存储在 .txt 文件中,但是当我在 href 下只有“click=yes”时它工作正常。 But I can't make it to track clicks when I have link to external site.但是当我链接到外部站点时,我无法跟踪点击次数。

Here is my code:这是我的代码:

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

My guess is it has to do something with header('Location: '. $_SERVER['SCRIPT_NAME']);我的猜测是它必须对 header('Location: '. $_SERVER['SCRIPT_NAME']); 做些什么but I can't figure it out so I could really use some help.但我想不通,所以我真的需要一些帮助。

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.我知道如何使用 MySQL 数据库来完成它,但我不能在将要实现它的地方使用它。

Thanks in advance!提前致谢! Cheers!干杯!

Your server never sees the URI being accessed as the client leaves your page. 当客户端离开您的页面时,您的服务器永远不会看到正在访问的URI 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>

(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) (在将外部网站的URL的GET组件传递到自己的页面时,请确保外部网站的URL是经过编码URL

Then in goto.php you store your click and send a redirect header 然后在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']);

Now you can track these clicks, you can add your domain-specific counters in goto.php instead of your text file 现在您可以跟踪这些点击,可以将特定于域的计数器添加到goto.php而不是文本文件中

You could use Javascript to catch click on a link , send data via AJAX call. 您可以使用Javascript捕获链接的点击,并通过AJAX调用发送数据。 Here is small sample using JQuery. 这是使用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));
}

I love the simple solution by Paul S., but if you want to track the clicks with date, you can do something like this:我喜欢 Paul S. 的简单解决方案,但如果您想跟踪带日期的点击次数,您可以这样做:

03/03/2022 14 2022 年 3 月 3 日 14
04/03/2022 2 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
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM