php 无 mysql 的唯一 ip 访问重定向

unique ip visit redirect in php without mysql

如果我的请求看起来很愚蠢,我很抱歉。 我已经为此找了很长时间,但没有运气。 基本上我想做的是:当访客访问我的 link'http://mysite. com/redirect .php' ,我的 php 脚本获取他的 ip 地址,检查它是否存在于存储在文件 'hits.txt' 中的 ips 数组中,如果存在则重定向他到另一页说'google.com' 如果没有,则将他的 IP 地址存储在文件中,然后将他重定向到另一个页面,比如 'yahoo.com'。 所以稍后当他再次访问时,他被重定向到 google.com。 当然,我的最终目的是制作一个唯一的 ip 访问脚本。 如果你知道如何在没有数据库和 sql 的情况下做到这一点,我将不胜感激,如果你认为它只能用 sql 来完成,那么请向我推荐最简单的方法。 到目前为止,我的代码不起作用:

<?php
// Unique Hits PHP Script
// ----------- March 2004
// Contact author: uniquehits@sizzly.com

$log = 'hits.txt';

$IP = getenv (REMOTE_ADDR);
$add = true;
$hits = 0;

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

$h = fopen ($log, 'r');



    if (in_array($IP, array($h))){


        header("Location: http://google.com");
    }
        else{
      $fp = fopen('hits.txt', 'a');
        fwrite($fp, "'" );
fwrite($fp, $IP );
fwrite($fp, "'" );
fwrite($fp, ',' );
fclose($fp);


        header("Location: http://yahoo.com");

        }





fclose($h);
?>

感谢你们。

您可以为此使用 cookie。 这可能是比使用 IP 地址更可靠的方法,因为很多人都有动态 IP。

http://php.net/manual/en/features.cookies.php

谢谢您,您帮了大忙,cookie 效果很好,非常感谢。 饼干 + php :)

这是代码,以防其他人需要它:

<!DOCTYPE html>

<html>
<body>

<?php
$IP = getenv (REMOTE_ADDR);
$verifier = "verify";
$cookie_value = $IP;
if(!isset($_COOKIE[$verifier])) {
   setcookie($verifier, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
   header("Location: http://google.com");
} else {
   header("Location: http://yahoo.com");
}
?>

</body>
</html>