【问题标题】:php maxmind geoip exception ipphp maxmind geoip 异常ip
【发布时间】:2013-07-20 18:36:28
【问题描述】:

我正在使用 Maxmid geoip 脚本 (php) 根据用户的位置重定向用户(www.mysite.com - 就本问题而言)。英国访问者将访问英国站点,而其他人将留在当前站点。但是,我还想为我的 ip 设置一个例外,以便我能够查看两个站点(前提是我的 ip 与文件中给出的匹配 - 我在英国)。下面是我的编码,它不适用于“异常 ip”。

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/geoip/geoip.inc");
$gi = geoip_open($_SERVER['DOCUMENT_ROOT']."/geoip/GeoIP.dat", GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$useragent_country = array('gb');
$exception_ip = "80.47.200.21";

if (in_array(strtolower($country), $useragent_country)){
    header('location: http://www.mysite.co.uk');
    exit();
} 
if ($_SERVER['REMOTE_ADDR'] = $exception_ip){
    header('location: http://www.mysite.com');
}
?>

另外,为了保持当前站点的“异常ip”,我的编码是否正确?

【问题讨论】:

    标签: php geoip maxmind


    【解决方案1】:

    最后我做到了。将异常 ip 放在一个数组中意味着我可以添加任意数量的。此外,$uri 意味着它会在重定向时转到相同的路径:

    <?php
    require_once($_SERVER['DOCUMENT_ROOT']."/geoip/geoip.inc");
    $gi = geoip_open($_SERVER['DOCUMENT_ROOT']."/geoip/GeoIP.dat", GEOIP_MEMORY_CACHE);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
    $useragent_country = array('gb');
    $uri = $_SERVER['REQUEST_URI'];
    $exception_ip = array("1.2.3.4.5");//ADD IPs here in array form
    
    if(in_array($_SERVER['REMOTE_ADDR'], $exception_ip)){
    
    //do nothing
    
    }
    else{
    if (in_array(strtolower($country), $useragent_country)){
        header('location: http://www.mysite.co.uk$uri');//redirect to same path
        exit();
    } 
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      相关资源
      最近更新 更多