View previous topic :: View next topic |
Author |
Message |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sat Jun 16, 2007 6:45 am Post subject: Validating an IP Range [Solved] |
|
|
Hello Everyone,
I was wondering if you could help me out with something. I would like to know how to check an IP Range so that I can write a simple IP Range Banning script. This will be for a major project I'm going to be working on and I want to make sure the code is valid.
I'm guessing the code could be like this?
Code: | <?php
if (127.0.0.0 <= 127.0.0.6) {
echo 'This IP Address Range is banned!';
}
?>
|
Any help on this would be wonderful. Maybe Aprelium could provide a function for how they validate their ranges.
Like this: http://www.aprelium.com/data/doc/2/abyssws-win-doc-html/ipformat.html
Note: The first 4 are very easy to do (Regular Expression) in PHP, but when it comes to the last ones, I have no clue what to do in order to validate them.
Kind regards, Josh.
Last edited by TRUSTAbyss on Sat Jun 16, 2007 7:51 pm; edited 2 times in total |
|
Back to top |
|
 |
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
|
Back to top |
|
 |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sat Jun 16, 2007 7:48 pm Post subject: |
|
|
Hello pkSML,
I looked at the user submited comments and found the function I'm looking for. The function doesn't support the Regex format but that's so easy to do without any help. The following function supports everything that Abyss supports!
Update: The code will now check for Wildcard characters to see if it's a Regex. Enjoy!
Function: netMatch()
Created By: Dirk, Stephane, and TRUSTAbyss
Supported Syntax:
- *
- 202.*
- 202.1.*
- 202.1.192.*
- 202.1.192.0-202.1.192.255: a range of IPs
- 200.36.161.0/24: a range of IP by using net masking
- 200.36.161/24: a shorten syntax similar to the above
Note: A single IP Address can also be used.
Code: | <?php
/**
* Function: netMatch()
* Created By: Dirk, Stephane, and Joshua H. (TRUSTAbyss)
*
* This function returns a boolean value.
* Usage: netMatch("IP RANGE", "IP ADDRESS")
*/
function netMatch($network, $ip)
{
$network = trim($network);
$ip = trim($ip);
$d = strpos($network, '-');
if (preg_match("/^\*$/", $network))
{
$network = str_replace('*', '^.+', $network);
}
if (!preg_match("/\^\.\+|\.\*/", $network))
{
if ($d === false)
{
$ip_arr = explode('/', $network);
if (!preg_match("/@\d*\.\d*\.\d*\.\d*@/", $ip_arr[0], $matches))
{
$ip_arr[0] .= '.0'; // Alternate form 194.1.4/24
}
$network_long = ip2long($ip_arr[0]);
$x = ip2long($ip_arr[1]);
$mask = long2ip($x) == $ip_arr[1] ? $x : (0xffffffff << (32 - $ip_arr[1]));
$ip_long = ip2long($ip);
return ($ip_long & $mask) == ($network_long & $mask);
}
else
{
$from = ip2long(trim(substr($network, 0, $d)));
$to = ip2long(trim(substr($network, $d+1)));
$ip = ip2long($ip);
return ($ip >= $from and $ip <= $to);
}
}
else
{
return preg_match("/$network/", $ip);
}
}
// Here's an Example
if (netMatch('127.0.0.0-127.0.0.4', $_SERVER['REMOTE_ADDR']))
{
echo 'This address range has been banned!';
}
?>
|
Note: I've tested the above code and it works perfectly! The Regex matches are accurate and the Ranges format also work good. I'm not sure if the function supports the last one that Abyss supports (aaa.bbb.ccc.ddd-iii.jjj.kkk.lll/n). Please test this function and let me know of any bugs. Thank You!
Kind regards, Josh |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|