Some simple processing of IP restrictions on php pages

In the past, I encountered some customers who required that certain pages can only be accessed by certain specific IPs. I tried several methods, but couldn't find them at once. When I was just sorting it out, I found one and recorded it first, then I found the others and edited them later.
1. Only specific IP segments can be accessed, but it must be a dynamic page

<?
if(!(strstr($_SERVER["REMOTE_ADDR"],"192.168.102.") ||strstr($_SERVER["REMOTE_ADDR"],"192.168.103.")  ||strstr($_SERVER["REMOTE_ADDR"],"192.168.111.") || strstr($_SERVER["REMOTE_ADDR"],"10.140.234.") || strstr($_SERVER["REMOTE_ADDR"],"192.168.105.") ||strstr($_SERVER["REMOTE_ADDR"],"192.168.106.")))
{
echo"您暂时没有访问权限";
exit();
}
?>

Only the IP segments in the code can be accessed.

<?php
if($ipkz!="*"){
    
    
	$isPass = false;
	$ipList = explode("\r\n", $ipkz);
	foreach($ipList as $ip){
    
    
		if(empty($ip)) continue;
		if(strstr($_SERVER["REMOTE_ADDR"],$ip)) {
    
    
			$isPass = true;
			break;
		}
	}
	if(!$isPass) exit("您暂无访问权限");
}
?>

<?php
if($cat.ipkz!="*"){
    
    
	$isPass = false;
	$ipList = explode("\r\n", $cat.ipkz);
	foreach($ipList as $ip){
    
    
		if(empty($ip)) continue;
		if(strstr($_SERVER["REMOTE_ADDR"],$ip)) {
    
    
			$isPass = true;
			break;
		}
	}
	if(!$isPass) exit("无权限访问!");
}
?>

Guess you like

Origin blog.csdn.net/likeni1314/article/details/99702525