Code in PHP that restricts access to IP segments and prohibits IP from submitting forms

  • 2020-05-05 10:58:39
  • OfStack

We simply add the following code to feedback.php to make a judgment.

Note: the following is an example of PHP limited IP code, if you want to apply CMS, please modify, or if you are using DEDECMS, you can contact this site.
 
<?php 
// 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']; 
} 
// limit ip 
if ($userip=='27.37.188.128'){ 
header("location:http://sc.jb51.net");// Jump to script house station after banned  
exit; 
} 
// limit ip Period of  
$ip_arr = explode('.', $userip); 
# The limit ip Segment, let's say 192.168.*.* 
if (!(($ip_arr[0] == '192' && $ip_arr[1]=='168') )){ 
header("location:http://sc.jb51.net");// Forbidden to jump to script home material station  
exit; 
}else{ 
header("location://www.jb51.net");// normal IP Direct access to the script home home page  
exit; 
} 
?> 

Related articles: