Home  >  Article  >  Backend Development  >  PHP code to restrict IP segment access and prohibit IP submission form_PHP Tutorial

PHP code to restrict IP segment access and prohibit IP submission form_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:50944browse

PHP code to restrict IP segment access and prohibit IP submission form

Recently, the editor discovered that a netizen from Yunnan often posted some spam comments on Bangkejia. Since www.bkjia.com is a DEDECMS architecture, the system itself does not have the function of banning IPs. I see these spam comments every day. , although it is not much, it makes people feel uncomfortable, so how to limit it? We only need to add the following code to feedback.php to make a judgment.

Note: The following is just an example code of PHP restricting IP. If you plan to apply it to CMS, please modify it yourself, or if you are using DEDECMS, you can contact this site.

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] //Add IP access restrictions
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_arr = explode('.', $userip);
#Restricted ip segment, assuming it is 192.168.*.*
if (!(($ip_arr[0] == '192' && $ip_arr[1] =='168') )){
header("http://www.liehuo.org");//Jump to Bangke’s home address navigation after being banned
exit;
} else
{
header("http://www.bkjia.com");//Normal IP will directly access the home page of Bangke Home
exit;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364763.htmlTechArticlePHP code to restrict access to IP segments and prohibit IP submission forms. Recently, the editor found that a netizen from Yunnan often Liehuo.com published some spam comments. Since www.veryhuo.com is DEDE...
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