Home  >  Article  >  Backend Development  >  Code to restrict IP segment access and prohibit IP submission form in PHP_PHP Tutorial

Code to restrict IP segment access and prohibit IP submission form in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:30:021031browse

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 code The code is as follows:

//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'];
}
//Restrict ip
if ($userip=='27.37.188.128'){
header("location:http://sc.jb51.net");//Jump to Script Home site after being banned
exit;
}
//Restrict IP segment
$ip_arr = explode('.', $userip);
#Restricted ip segment, assuming it is 192.168.*.*
if (!(($ip_arr[0] == '192' && $ ip_arr[1]=='168') )){
header("location:http://sc.jb51.net");//Jump to Script Home Material Station after being banned
exit ;
}else{
header("location:http://www.jb51.net");//Normal IP will directly access the Script Home homepage
exit;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323270.htmlTechArticleWe only need to add the following code to feedback.php for 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...
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