Skip to content

Instantly share code, notes, and snippets.

@stklcode
Created April 13, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stklcode/59447dd8419ad79d2bcd5d38d733ea65 to your computer and use it in GitHub Desktop.
Save stklcode/59447dd8419ad79d2bcd5d38d733ea65 to your computer and use it in GitHub Desktop.
Modified ip_in_range() method for the "Limit Login Attempts Reloaded" WP plugin.
public function ip_in_range( $ip, $list ) {
$bytes_ip = inet_pton( $ip );
foreach ( $list as $range ) {
$range = array_map( 'trim', explode( '-', $range ) );
if ( count( $range ) == 1 ) {
$bytes_range = inet_pton( $range[0] );
if ( $bytes_ip === $bytes_range ) {
return true;
}
} else {
$low = inet_pton( $range[0] );
$high = inet_pton( $range[1] );
if ( $low === false || $high === false || $bytes_ip === false ) {
continue;
}
$low = bin2hex( inet_pton( $low ) );
$high = bin2hex( inet_pton( $high ) );
$needle = bin2hex( $bytes_ip );
if ( $needle >= $low && $needle <= $high ) {
return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment