jetpack_protect_ip_address_is_in_range

Checks that a given IP address is within a given low - high range.

Description

jetpack_protect_ip_address_is_in_range( $ip, $range_low, $range_high ); 

Servers that support inet_pton will use that function to convert the ip to number, while other servers will use ip2long.

NOTE: servers that do not support inet_pton cannot support ipv6.

Parameters (3)

0. $ip
The ip.
1. $range_low
The range low.
2. $range_high
The range high.

Usage

  1. if ( !function_exists( 'jetpack_protect_ip_address_is_in_range' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/protect/shared-functions.php'; 
  3.  
  4. // The ip. 
  5. $ip = null; 
  6.  
  7. // The range low. 
  8. $range_low = null; 
  9.  
  10. // The range high. 
  11. $range_high = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = jetpack_protect_ip_address_is_in_range($ip, $range_low, $range_high); 
  15.  

Defined (1)

The function is defined in the following location(s).

/modules/protect/shared-functions.php  
  1. function jetpack_protect_ip_address_is_in_range( $ip, $range_low, $range_high ) { 
  2. // inet_pton will give us binary string of an ipv4 or ipv6 
  3. // we can then use strcmp to see if the address is in range 
  4. if ( function_exists( 'inet_pton' ) ) { 
  5. $ip_num = inet_pton( $ip ); 
  6. $ip_low = inet_pton( $range_low ); 
  7. $ip_high = inet_pton( $range_high ); 
  8. if ( $ip_num && $ip_low && $ip_high && strcmp( $ip_num, $ip_low ) >= 0 && strcmp( $ip_num, $ip_high ) <= 0 ) { 
  9. return true; 
  10. // ip2long will give us an integer of an ipv4 address only. it will produce FALSE for ipv6 
  11. } else { 
  12. $ip_num = ip2long( $ip ); 
  13. $ip_low = ip2long( $range_low ); 
  14. $ip_high = ip2long( $range_high ); 
  15. if ( $ip_num && $ip_low && $ip_high && $ip_num >= $ip_low && $ip_num <= $ip_high ) { 
  16. return true; 
  17.  
  18. return false; 
  19.