简体   繁体   中英

PHP - how to match if the IPv4 IP is in the range of IPs?

I have few spammer's visiting my site from: 4.53.111.76 or 4.53.111.1 or 4.53.1.76

Now how do i match with my private firewall where i want to block any IP range from 4.53.0.0 till 4.53.255.255?

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
$ip  = getRealIpAddr();
$abuse_report = urlencode("https://www.abuseat.org/lookup.cgi?ip={$ip}");

If you want to block at PHP end, there is already an answer to get IP ranges from start and end IPs

You may store those IP ranges in database or PHP array.

when user kicks the website, you get the users IP(As you already did) and check the user's IP with the stored IP ranges.

If the IP is matched with black listed then you can block the content to that user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM