/** * 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