Created
June 20, 2012 15:17
-
-
Save phette23/2960425 to your computer and use it in GitHub Desktop.
PHP function to check if IP falls within a range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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