Skip to content

Instantly share code, notes, and snippets.

@phette23
Created June 20, 2012 15:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phette23/2960425 to your computer and use it in GitHub Desktop.
Save phette23/2960425 to your computer and use it in GitHub Desktop.
PHP function to check if IP falls within a range
<?php
function is_on_campus($IP = false) {
// If no IP is passed grab it from $_SERVER[]
$IP = $IP ? $IP : $_SERVER['REMOTE_ADDR'];
// Convert IP to long integer
$IPnum = ip2long($IP);
// Test against (made up) campus IP ranges
if (
( $IPnum >= ip2long( "111.11.11.11" ) && $IPnum <= ip2long( "111.11.11.22" ) ) ||
( $IPnum >= ip2long( "222.22.22.22" ) && $IPnum <= ip2long( "222.22.22.33" ) ) ||
// made-up proxy server
$IPnum === ip2long( "33.33.33.33" )
)
{
return true;
}
else return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment