【问题标题】:unique ip visit redirect in php without mysql没有mysql的php中唯一的IP访问重定向
【发布时间】:2016-11-10 08:13:41
【问题描述】:

如果我的要求看起来很愚蠢,我很抱歉。 我已经为此寻找了很长时间,但没有运气。 基本上我想做的是:当访问者访问我的链接时,我想要'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);
?>

感谢你们。

【问题讨论】:

  • 确实知道访问者和某个 IP 地址之间没有明确的相关性吗?那么基于不成立的假设来实现逻辑有什么意义呢?从您的角度来看,许多访问者可能共享相同的 IP 地址。并且单个访问者可能使用不同的 IP 地址,甚至在同一时间。
  • 使用数据库,随着文件的增长,这将变得非常慢
  • 好吧,我从流量交换网站获得流量,我想重定向该流量,如果 IP 地址已经访问过我的网站,那么如果他再次访问,我想将他重定向到另一个网站/页面.
  • @Dragon,我正在考虑使用 cron 作业,每 24 小时使文件 hits.txt 无效。 ;)

标签: php arrays ip unique visitor-pattern


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    谢谢你,你帮了大忙,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>
    
    
    
    
    
    
    
    
    
    
       

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      相关资源
      最近更新 更多