CFLib.org – Common Function Library Project

IPConvert(ip or numeric version)

Last updated August 28, 2003

Version: 1 | Requires: ColdFusion 5 | Library: NetLib

 
Rated 2 time(s). Average Rating: 4.5

Description:
IPConvert converts valid IP addresses into integers and back again. This is useful for storing IP information in a database using the int datatype rather than char.

Return Values:
Returns either a number of an IP.

Example:

view plain print about
<cfset int=IPConvert(CGI.REMOTE_ADDR)>
<cfset ip=IPConvert(int)>
<cfoutput>
Converted to: #int#<br>
Converted back to: #ip#
</cfoutput>

Parameters:

Name Description Required
ip or numeric version IP or numeric version of IP. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Converts IPs to integers and back for efficient database storage.
 * 
 * @param ip or numeric version      IP or numeric version of IP. (Required)
 * @return Returns either a number of an IP. 
 * @author Aaron Eisenberger (aaron@x-clothing.com) 
 * @version 1, August 28, 2003 
 */

function IPConvert(val) {
    var int = '';
    var ip = arraynew(1);
    if (find('.',val))
        {
        int = 0;
        int = ListGetAt(val, 1, ".") * 256^3;
        int = int + ListGetAt(val, 2, ".") * 256^2;
        int = int + ListGetAt(val, 3, ".") * 256;
        int = int + ListGetAt(val, 4, ".");
        return int;
        }
    else
        {
        int = val;
        ip[1] = Int(int / 256^3);
        int = int - (ip[1] * 256^3);
        ip[2] = int(int / 256^2);
        int = int - (ip[2] * 256^2);
        ip[3] = int(int / 256);
        ip[4] = int - (ip[3] * 256);
        ip = ip[1] & "." & ip[2] & "." & ip[3] & "." & ip[4];
        return ip;
        }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
15 day(s) ago

James Sleeman James Sleeman added
quickSort
25 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
28 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson