我目前使用PHP调用一个geoip数据库,根据用户的IP地址解析到的状态来重定向用户。
我现在切换到一个调用maxmind数据库的javascript API。
问题:我不知道如何将我自己的IP地址列入白名单。
以下是不再使用的旧PHP代码:
include_once("/home/censor/geoip/geoipcity.inc");
$gi = geoip_open("/home/censor/geoip/GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$_SERVER["REMOTE_ADDR"]);
geoip_close($gi);
if((trim($record->region)=="WA") && ($_SERVER["REMOTE_ADDR"]!="11.111.111.111")) {
header("Location: http://www.google.com"); /* Redirect browser */
exit;
}11.111.111.111是白名单中的IP地址。
下面是新的javascript代码:
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script type="text/javascript">
var redirect = (function () {
/* This implements the actual redirection. */
var redirectBrowser = function (site) {
var uri = "http://" + site + ".google.com/";
window.location = uri;
};
/* These are the country codes for the countries we have sites for.
* We will check to see if a visitor is coming from one of these countries.
* If they are, we redirect them to the country-specific site. If not, we
* redirect them to world.example.com */
var sites = {
"WA": true
};
var defaultSite = "www";
var onSuccess = function (geoipResponse) {
/* There's no guarantee that a successful response object
* has any particular property, so we need to code defensively. */
if (!geoipResponse.city.iso_code) {
redirectBrowser("www");
return;
}
/* ISO country codes are in upper case. */
var code = geoipResponse.city.iso_code.toLowerCase();
if ( sites[code] ) {
redirectBrowser(code);
}
else {
redirectBrowser("www");
}
};
/* We don't really care what the error is, we'll send them
* to the default site. */
var onError = function (error) {
redirectBrowser("www");
};
return function () {
geoip2.city( onSuccess, onError );
};
}());
redirect();
</script>是否有人能够为我提供一种使用新代码将IP地址列入白名单的方法?我对PHP或Javascript一点也不流利,所以非常感谢你的帮助。
谢谢
发布于 2014-11-01 11:21:58
如果这段javascript代码在PHP文件中是内联的(听起来就是这样),您可以替换以下代码:
if ( sites[code] ) {有了这个:
if (sites[code] && "<?php echo $_SERVER['REMOTE_ADDR']; ?>" !== "11.111.111.111") {OP已指定javascript代码不是内联的。在这种情况下,您可以从以下位置修改代码:
if ( sites[code] ) {要这样做:
if (sites[code] && userIp !== "11.111.111.111") {其中,11.111.111.111为您要加入白名单的IP。然后,在PHP中包含以下脚本:
<script>
var userIp = "<?php echo $_SERVER['REMOTE_ADDR']; ?>";
</script>https://stackoverflow.com/questions/26685681
复制相似问题