开发者

How to change the value which is being counted in this unique hits counter?

开发者 https://www.devze.com 2023-02-28 15:19 出处:网络
I have this unique hit counter: <?php $log = \'hits.log\'; $IP = getenv (REMOTE_ADDR); $add = true; $hi开发者_StackOverflow社区ts = 0;

I have this unique hit counter:

<?php
$log = 'hits.log';

$IP = getenv (REMOTE_ADDR);
$add = true;
$hi开发者_StackOverflow社区ts = 0;

if (!file_exists ($log)) {
    echo "Error: $log does not exist.";
    exit;
}

$h = fopen ($log, 'r');
while (!feof ($h)) {
    $line = fgets ($h, 4096);
    $line = trim ($line);
    if ($line != '')
        $hits++;

    if ($line == $IP)
        $add = false;
}

fclose($h);

if ($add == true) {
    $h = fopen ($log, 'a');
    fwrite($h, "
$IP");
    fclose($h);
    $hits++;
}

echo $hits;
?>

But it only counts the unique hits the page i put it on gets. So if i put this code on http://site.com it will only display the unique hits for http://site.com. I want to know if i can add or edit something in the code to make the script count the unique hits for a different page, but display it on the page it is on ( track http://site.com/yay but display clicks on http://site.com) Anyone know how to do this? Thanks.


Just for example (requested in comments)

<?php
$log = 'hits.log';

if (!file_exists ($log)) {
    echo "Error: $log does not exist.";
    exit;
}

$hits = (int) file_get_contents($log);

if (!isset($_COOKIE['last_visit']) or date('d.m.Y', (int) $_COOKIE['last_visit']) != date('d.m.Y')) {
    setcookie('last_visit', time(), 90000); //of course you can encode it for security reason

    $hits++;
    file_put_contents($log, $hits);
}

echo $hits;
?>

This is still not the best way to count your users.

0

精彩评论

暂无评论...
验证码 换一张
取 消