Skip to content

Instantly share code, notes, and snippets.

@geeksunny
Created April 26, 2012 20:02
Show Gist options
  • Save geeksunny/2502609 to your computer and use it in GitHub Desktop.
Save geeksunny/2502609 to your computer and use it in GitHub Desktop.
A PHP function to determine if a given IP address lies within a given range.
<?php
function check_ip_range($target, $range_start, $range_end)
{
if (ip2long($target) >= ip2long($range_start) && ip2long($target) <= ip2long($range_end))
return true;
else
return false;
}
// Should return false
var_dump(check_ip_range("127.0.0.1","10.0.0.0","10.255.255.255"));
// Should return true
var_dump(check_ip_range("10.6.0.134","10.0.0.0","10.255.255.255"));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment