ASP and PHP restrict IP access to allow only specified IP access allow * Number wildcard filter IP

Source: Internet
Author: User
Tags explode servervariables

/** * Detect if IP access is required for the permitted IP * Enter description here ...*/functioncheck_ip () {$ALLOWED _ip=Array(' 192.168.2.* ', ' 127.0.0.1 ', ' 192.168.2.49 '); $IP=GetIP (); $check _ip_arr=Explode(‘.‘,$IP);//the IP to be detected is split into an array    #Limit IP    if(!In_array($IP,$ALLOWED _ip)) {        foreach($ALLOWED _ip  as $val){            if(Strpos($val, ' * ')!==false){//found an * number substitute                 $arr=Array();//                 $arr=Explode(‘.‘,$val); $BL=true;//used to record whether a match was successful in the loop detection                  for($i= 0;$i<4;$i++){                     if($arr[$i]!= ' * ') {//Not equal to * will come in detection, if the symbol substitute is not checked                         if($arr[$i]!=$check _ip_arr[$i]){                             $BL=false;  Break;//Terminate check this IP continue check next IP                         }                     }                 }//End for                 if($BL){//If true, find a match that succeeds and returns                     return;  die; }            }        }//End foreach        Header(' http/1.1 403 Forbidden '); Echo"Access Forbidden";  die; }}
function GetIP () {    returnisset($_server["http_x_forwarded_for"])?  $_server["Http_x_forwarded_for"]    :(isset($_server["Http_client_ip"])?  $_server["Http_client_ip"]    :$_server["REMOTE_ADDR"]);}

The following is the ASP version:

<%'A collection of masked IP addresses (segments) with the asterisk as a wildcard, usually saved in a configuration file. ConstBadipgroup ="192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1"IfIsforbidip (Badipgroup) =True  ThenResponse.Write (GetIP&"IP Address Forbidden Access") Response.End ()End If'parameter Vbadip: IP segment to block, IP address collection, multiple IP addresses separated by | symbol (segment)'returns the Bool:true User IP in the masked range, False insteadFunctionIsforbidip (VBADIP)Dimcounter, Arrippart, Arrbadip, Arrbadippart, I, J Arrbadip=Split(Vbadip,"|") Arrippart=Split(GetIP (),".")     fori =0  to UBound(ARRBADIP) counter=0Arrbadippart=Split(Arrbadip (i),".")     forj =0  to UBound(Arrippart)If(Arrbadippart (j)) ="*" or Cstr(Arrippart (j)) =Cstr(Arrbadippart (j)) Thencounter= Counter +1    End If    Next    IfCounter =4  ThenIsforbidip=True    Exit Function    End If    NextIsforbidip=FalseEnd Function'' return customer IP addressFunctionGetIP ()DimIP IP= Request.ServerVariables ("http_x_forwarded_for")    IfIP =""  ThenIP = Request.ServerVariables ("REMOTE_ADDR") GetIP=IPEnd Function%>

ASP and PHP restrict IP access to allow only specified IP access allow * Number wildcard filter IP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.