Home >Backend Development >PHP Tutorial >php中限制ip段访问、禁止ip提交表单的代码分享_php技巧

php中限制ip段访问、禁止ip提交表单的代码分享_php技巧

WBOY
WBOYOriginal
2016-05-16 20:37:21839browse

在需要禁止访问或提交表单的页面添加下面的代码进行判断就可以了。

注意:下边只是一个PHP限制IP的实例代码,如果您打算应用到CMS中,请自行修改。

<&#63;php 
/加IP访问限制 
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { 
$userip = getenv('HTTP_CLIENT_IP'); 
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { 
$userip = getenv('HTTP_X_FORWARDED_FOR'); 
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { 
$userip = getenv('REMOTE_ADDR'); 
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { 
$userip = $_SERVER['REMOTE_ADDR']; 
} 
//限制ip 
if ($userip=='192.168.1.88'){ 
header("location:http://t.qq.com/wb631992791");//被禁止后跳转到微博
exit; 
} 
//限制ip段 
$ip_arr = explode('.', $userip); 
#限制的ip段,假设是192.168.*.* 
if (!(($ip_arr[0] == '192' && $ip_arr[1]=='168') )){ 
header("location:http://t.qq.com/wb631992791");//被禁止后跳转到微博
exit; 
}else{ 
header("location:http://afish.cnblogs.com");//正常IP则直接访问小鱼阁首页 
exit; 
} 
&#63;> 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn