Support » Plugin: NinjaFirewall (WP Edition) - Advanced Security » Blocking an IP range

  • How can I block an IP range in the .htninja file?

    example: ip range 180.170.160.150 – 181.171.161.151

    I have understand that there is a variable for a single range:

    “// Whitelist all IPs from 1.1.1.1 to 1.1.1.255:
    if ( preg_match( ‘/^1\.1\.1\.\d+$/’, $_SERVER[“REMOTE_ADDR”] ) ) {
    return ‘ALLOW’; // whitelist
    }”

    and if I write BLOCK instead allow the range will be blocked, but how to this with a larger range?

Viewing 1 replies (of 1 total)
  • Plugin Author nintechnet

    (@nintechnet)

    You can convert your two IP addresses to a long integer with the ip2long PHP function, then compare the visitor’s IP to these 2 values:

    <?php
    /*
     +====================================================================+
     | NinjaFirewall optional configuration file                          |
     |                                                                    |
     | See: https://nintechnet.com/ninjafirewall/wp-edition/help/?htninja |
     +====================================================================+
    */
    $low = '3031081110'; // ip2long( '180.170.160.150' );
    $high = '3047924119'; // ip2long( '181.171.161.151' );
    
    if ( isset( $_SERVER["REMOTE_ADDR"] ) ) {
       $ip = ip2long( $_SERVER["REMOTE_ADDR"] );
       if ( $ip >= $low && $ip <= $high ) {
          return 'BLOCK'; // reject it
       }
    }
    
Viewing 1 replies (of 1 total)
  • The topic ‘Blocking an IP range’ is closed to new replies.