Home >Backend Development >PHP Tutorial >How to set up a certain IP segment in the background to allow login in PHP? _PHP Tutorial
Sometimes we hope that the IP address of the Changsha address of the website can be logged in but the IP addresses in other regions cannot. Now I will write a php program for you to limit the ability of a certain IP segment in the background to log in.
代码如下 | 复制代码 |
public function login() { //设定IP段登录 if(getenv("HTTP_CLIENT_IP")) { $onlineip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR')){ $onlineip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR')){ $onlineip = getenv('REMOTE_ADDR'); } else{ $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR']; } // echo $onlineip; $adminip = $onlineip; $arrayip = array('117.121.48.*','127.0.0.*');//ip段 $ipregexp = implode('|', str_replace( array('*','.'), array('d+','.') ,$arrayip) ); $allow = preg_match("/^(".$ipregexp.")$/", $adminip); if ($allow != 1) { showmessage(L('你的ip地址不在被允许的范围内!'),'?m=admin&c=index&a=login',6000); } |
Put the following code into the backend login portal of your website and call it, then change m=admin&c=index&a=login to your address.