簡體   English   中英

單擊鏈接時單擊計數器 PHP/JS

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

我這里有一個小腳本,可以在單擊鏈接時計算點擊次數並將其存儲在 .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 數據庫來完成它,但我不能在將要實現它的地方使用它。

提前致謝! 干杯!

當客戶端離開您的頁面時,您的服務器永遠不會看到正在訪問的URI 為此,最好設置一個像這樣的重定向

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

(在將外部網站的URL的GET組件傳遞到自己的頁面時,請確保外部網站的URL是經過編碼URL

然后在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而不是文本文件中

您可以使用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));
}

我喜歡 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
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM