简单的访客柜台 [英] simple visitor counter

查看:59
本文介绍了简单的访客柜台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面中集成了简单的访客计数器脚本(php)(我知道它非常不准确且很糟糕,但是目前仅用于测试):

I have simple visitor counter script ( php ) integrated in my page (I know it is very inaccurate and bad, but using only for testing at this moment):

<?php
$file = "counts.html";

//if cookie isn't already set,then increase counts by one + save ip, and set a cookie to pc...
$cookie_namee='mycounterr-456';
if (!isset($_COOKIE[$cookie_namee])) {
    file_put_contents($file, ( !file_exists($file) ? 0: (file_get_contents($file)+1) ) );
    setcookie($cookie_namee, "Checked", time()+99999, '/');
}
?>

是否也有办法获得访问者的IP? (回显$ GET_REMOTE_IP;不起作用)。

Is there a way to get IP of visitor too? (echo $GET_REMOTE_IP; doesnt work).

推荐答案

由于我找不到令人满意的足够简单的解决方案,所以我来了与我自己的。创建一个名为 ip.txt 的空文件,并在代码中的某个位置使用它:

Since I didn't find a satisfying "simple enough" solution, I came up with my own. Create an empty file called ip.txt and use this somewhere in your code:

$ip_all = file("ip.txt");
$ip_cur = $_SERVER['REMOTE_ADDR']."\n";
if (!in_array($ip_cur, $ip_all)) {
    $ip_all[] = $ip_cur;
    file_put_contents("ip.txt", implode($ip_all));
}

echo "visitors: " . count($ip_all);

请注意,此文件会随着时间的推移而变大,具体取决于自访问以来获得的访问者数量条目不会过期并且会像Cookie一样被删除。但是,正如已经提到的,我希望它尽可能简单,不要在意。另外,我也不想依靠cookie,因为我怀疑网络爬虫和其他机器人会将它们发送回去。

Note that this file will can get somewhat large over time depending on the amount of visitors you get since the entries don't expire and get deleted like cookies. But as already mentioned, I wanted it to be as simple as possible and don't care about that. Also I don't want to rely on cookies because I doubt web-crawlers and other robots will send them back.

这篇关于简单的访客柜台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆