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.
精彩评论