[英]php maxmind geoip exception ip
I'm using the Maxmid geoip script (php) to redirect users based on their location (www.mysite.com - for the purposes of this question). 我正在使用Maxmid geoip脚本(php)根据用户的位置重定向用户(www.mysite.com-就此问题而言)。 uk visitors will go to the uk site, whereas others will stay on the current site.
英国访问者将访问英国站点,而其他访问者将停留在当前站点上。 however, i also want to set up an exception, just for my ip so that i am able to view both sites (provided my ip matches up with that given in the file - i am based in the UK).
但是,我也想为我的IP设置一个例外,以便能够查看两个站点(前提是我的IP与文件中给出的IP匹配-我位于英国)。 below is my coding which doesn't work with regard to the "exception ip".
以下是我的编码,对于“ exception 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');
}
?>
furthermore, to keep the "exception ip" on the current site, is my coding correct? 此外,要在当前站点上保留“ exception ip”,我的编码是否正确?
I did it in the end. 我最终做到了。 putting the exception ip in an array means i can add as many as i want.
将异常ip放入数组中意味着我可以添加任意多个。 also, the
$uri
means it will go to the same path upon redirect: 同样,
$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();
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.