Home  >  Article  >  Backend Development  >  How to set up a certain IP segment in the background to allow login in PHP? _PHP Tutorial

How to set up a certain IP segment in the background to allow login in PHP? _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:35939browse

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633159.htmlTechArticleSometimes we hope that the IP of the Changsha address of the website can be logged in but the IP of other regions cannot. Let me write it for you below. A php program can limit the ability to log in to a certain IP segment in the background...
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