Fork me on GitHub

屏蔽指定地区IP访问

<?php

if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])  
{  
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];  
}  
elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])  
{  
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];  
}  
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])  
{  
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];  
}  
elseif (getenv("HTTP_X_FORWARDED_FOR"))  
{  
$ip = getenv("HTTP_X_FORWARDED_FOR");  
}  
elseif (getenv("HTTP_CLIENT_IP"))  
{  
$ip = getenv("HTTP_CLIENT_IP");  
}  
elseif (getenv("REMOTE_ADDR"))  
{  
$ip = getenv("REMOTE_ADDR");  
}  
else  
{  
$ip = "Unknown";  
}  

$query = (file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='. $ip ));
$de_json = json_decode($query, true); 
if($de_json['data']['country'] == "中国" && $de_json['data']['region'] != "香港" && $de_json['data']['region'] != "台湾" && substr($_SERVER['REQUEST_URI'],1,4) !='link')
{ 
?>
  你的地区不可访问
<?php
  exit();
}


        //  PUBLIC_PATH
define('PUBLIC_PATH', __DIR__);


// Bootstrap
require PUBLIC_PATH.'/../bootstrap.php';


// Init slim routes
require BASE_PATH.'/config/routes.php';
    

替换到public的index.php即可.

posted @ 2019-05-19 08:53  replaceroot  阅读(1725)  评论(0编辑  收藏  举报