Click counter when link is clicked PHP/JS

早过忘川 提交于 2019-12-18 09:47:10

问题


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.

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']); 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.

Thanks in advance! Cheers!


回答1:


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>

(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)

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']);

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




回答2:


You could use Javascript to catch click on a link , send data via AJAX call. Here is small sample using 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));
}


来源:https://stackoverflow.com/questions/26087296/click-counter-when-link-is-clicked-php-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!