Home > php教程 > php手册 > body text

PHP中限制IP段访问、禁止IP提交表单的代码

WBOY
Release: 2016-06-06 20:36:46
Original
857 people have browsed it

最近,小编发现有一个云南的网友经常在网站发表一些垃圾信息的评论,由于使用的事DEDECMS构架,系统本身并无禁止IP功能,每天看到这些垃圾评论,尽管不多,但是让人感觉不爽,那么如何来限制呢?

我们只要在feedback.php中添加下面的代码进行判断就可以了。

注意:下边只是一个PHP限制IP的实例代码,如果您打算应用到CMS中,请自行修改,或者如果您正在使用DEDECMS,可以联系本站。
代码如下:
//加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=='27.37.188.128'){
header("location:http://sc.jb51.net");//被禁止后跳转到脚本之家站
exit;
}
//限制ip段
$ip_arr = explode('.', $userip);
#限制的ip段,假设是192.168.*.*
if (!(($ip_arr[0] == '192' && $ip_arr[1]=='168') )){
header("location:http://sc.jb51.net");//被禁止后跳转到脚本之家素材站
exit;
}else{
header("location:http://www.jb51.net");//正常IP则直接访问脚本之家首页
exit;
}
?>
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!