I convert IPs to numbers anytime I deal with ranges. It makes it much easier
and more efficient to work with ranges. I don't mean that I convert them to
their actual decimal values, I just make sure each octet is 3 digits and
remove the (.)
It's much simpler than converting them to their decimal values or binary
equivalents but it is a one way conversion since you won't be able to
convert it back to an IP address without knowing where the dots go.
Examples:
192.168.0.1 = 192168000001
192.168.0.50 = 192168000050
192.168.0.255 = 192168000255
10.0.0.1 = 010000000001
10.0.0.255 = 010000000255
I store the actual IP and its number in the database so I can just use
simple number comparison in my SQL to get IPs within certain ranges or
whatever. But, like I said, since you can't always convert the number back
to the IP address, you would need to store the IP as well.
Here are some functions I wrote the other day for use in my current project.
Maybe you or someone can get some use out of them.
Iptonumber() converts an IP to the above format
validIPRange() uses iptonumber() to convert a start and ending ip to numbers
and ensure the starting IP is less than the ending IP address, thus ensuring
a valid range
validIP() just ensures that an IP address is between 1.0.0.0 and
255.255.255.255
ipisinrange() takes 3 IP addresses, uses iptonumber() to convert them to
numbers then ensures that IP1 is between IP2 and IP3
/************************************************************************/
/**
* Converts an ip address to manageable number
*
* @param ip Ip Address to convert. (Required)
* @return Returns a number/string value (12 digits).
* @author Bobby Hartsfield ([EMAIL PROTECTED])
* @version 1
* @created October 31, 2007
*/
function iptonumber(ip) {
var o = 1;
var ipnum = '';
for (o=1;o lte 4; o=o+1)
{
ipnum = ipnum & numberformat(listgetat(ip, o, '.'), '009');
}
return ipnum;
}
/************************************************************************/
/************************************************************************/
/**
* Makes sure that the passed in IPs are a valid range (startip is lower
* than the endip)
*
****REQUIRES IPTONUMBER()***
*
* @param startip First IP in range (Required)
* @param endip First IP in range (Required)
* @return Returns true or false.
* @author Bobby Hartsfield ([EMAIL PROTECTED])
* @version 1
* @created October 31, 2007
*/
function validiprange(startip,endip)
{
if (iptonumber(startip) lt iptonumber(endip))
{ return true; }
else
{ return false; }
}
/************************************************************************/
/************************************************************************/
/**
* Ensures that the passed in IP address is
* between 1.0.0.0 and 255.255.255.255
*
* @param ip Ip Address to convert. (Required)
* @return Returns true or false.
* @author Bobby Hartsfield ([EMAIL PROTECTED])
* @version 1
* @created October 31, 2007
*/
function isvalidip(ip)
{
if (listlen(ip, '.') neq 4) {return false;}
else if (val(listgetat(ip, 1, '.')) lte 0 or val(listgetat(ip, 1,
'.')) gt 255) {return false;}
else if (val(listgetat(ip, 2, '.')) lt 0 or val(listgetat(ip, 2,
'.')) gt 255) {return false;}
else if (val(listgetat(ip, 3, '.')) lt 0 or val(listgetat(ip, 3,
'.')) gt 255) {return false;}
else if (val(listgetat(ip, 4, '.')) lt 0 or val(listgetat(ip, 4,
'.')) gt 255) {return false;}
else { return true; }
}
/************************************************************************/
/************************************************************************/
/**
* returns true or false to indicate whether or not an IP is within a
* range of IPs
*
****REQUIRES IPTONUMBER()***
*
* @param ip IP to test (Required)
* @param startip starting IP of the range (Required)
* @param endip ending IP of the range (Required)
* @return Returns true or false.
* @author Bobby Hartsfield ([EMAIL PROTECTED])
* @version 1
* @created October 31, 2007
*/
function ipisinrange(ip,startip,endip)
{
if (iptonumber(ip) gte iptonumber(startip) and iptonumber(ip) lte
iptonumber(endip))
{ return true; }
else
{ return false; }
}
/************************************************************************/
..:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
Bobby Hartsfield
http://acoderslife.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Check out the new features and enhancements in the
latest product release - download the "What's New PDF" now
http://download.macromedia.com/pub/labs/coldfusion/cf8_beta_whatsnew_052907.pdf
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:292690
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4