Block specific IP block from my website in PHP

前端 未结 8 1524
小蘑菇
小蘑菇 2020-12-05 12:02

I\'d like, for example, block every IP from base 89.95 (89.95..). I don\'t have .htaccess files on my server, so I\'ll have to do it with PHP.

8条回答
  •  渐次进展
    2020-12-05 12:13

    Try strpos()

    if(strpos($_SERVER['REMOTE_ADDR'], "89.95") === 0)
    {
        die();
    }
    

    If you notice, the === operator makes sure that the 89.95 is at the begining of the IP address. This means that you can sepcify as much of the IP address as you want, and it will block no matter what numbers come after it.

    For instance, all of these will be blocked:

    89.95 -> 89.95.12.34, 89.95.1234.1, 89.95.1.1
    89.95.6 -> 89.95.65.34, 89.95.61.1, 89.95.6987

    (some of those aren't valid IP addresses though)

提交回复
热议问题