Several methods of shielding IP list

Source: Internet
Author: User
Tags explode sprintf strcmp

Below I collect several commonly used shielding IP list of program processing method, shielding this can make your website better, more stable, below we do not analyze the following shielding method.

$ip _str = "127.0.0.1,192.1.1.*,202.*,127.*,123.*,142.*,*";
$ip = "127.0.0.254";
function Stopip ($IP, $ip _str)
{
if (strcmp (Long2ip (sprintf ('%u ', Ip2long ($IP)), $ip)) return false;//Verify IP if not IP format returns false
$ip = Explode (".", $ip);
if (eregi (, *,|,*.|,*) | (". $IP [0]. ". (". $ip [1]." | *).? (". $ip [2]." | *)?.? (". $ip [3]." | *) ",", ". $ip _str.", ")" return true;
}
if (Stopip ($ip, $ip _str)) echo 1;


function Stopip ($IP, $ip _str)
{
if (strcmp (Long2ip (sprintf ('%u ', Ip2long ($IP)), $ip)) return false;//Verify IP if not IP format returns false
$ip = Explode (".", $ip);
if (eregi (, *,|,*.|,*) |, (". $ip [0].". (". $ip [1]." | *).? (". $ip [2]." | *)?.? (". $ip [3]." | *),?) ",", ". $ip _str.", ")" return true;
}

function Stopip ($IP, $STR)
{
if (Stristr (",". $str. ",", $ip)) return true;//if it is not allowed in the range
if (Stristr (",". $str. ",", ", *,") | | stristr (",". $str. ",", ", *")) return true;//if the first appearance, *. Or, * means that all sites are not allowed
$iplist = Explode (",", $str);
$ip = Explode (".", $ip);
for ($i = 0; $i <count ($iplist); $i + +)
{
if (eregi ($ip [0].). (". $ip [1]." | *).? (". $ip [2]." | *)?.? (". $ip [3]." | *)? ", $iplist [$i]) return true;
}
}
Test
$ip _str = "127.0.0.1,192.1.1.*,202.*,127.*,123.*";//This is the screened IP list to "," separated, if you want to block all IP please use * If you want to block 127. The IP format at the beginning is 127.*
$ip = "137.0.0.2"; IP that needs to be validated
if (Stopip ($ip, $ip _str)) echo 1;


For ($i =1 $i <=255; $i + +)
{
For ($k =1 $k <=255; $K + +)
{
For ($a =1 $a <=255; $a + +)
{
$str. = "192. {$i}. {$k}. {$a} ";
}
}
}
This will produce a large amount of 255*255*255 IP data.

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.