简体   繁体   English

PHP-如果IPv4 IP在IP范围内,如何匹配?

[英]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 我很少有垃圾邮件发送者从以下位置访问我的网站:4.53.111.76或4.53.111.1或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? 现在如何与我的私有防火墙匹配,在该防火墙中我要阻止从4.53.0.0到4.53.255.255的任何IP范围?

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 如果要在PHP端阻止,已经有一个答案可以从起始IP和结束IP获取IP范围

You may store those IP ranges in database or PHP array. 您可以将这些IP范围存储在数据库或PHP数组中。

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. 当用户打开网站时,您将获得用户的IP(就像已经做的那样),并使用存储的IP范围检查用户的IP。

If the IP is matched with black listed then you can block the content to that user 如果IP与列出的黑色匹配,则可以将内容阻止给该用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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