CFLib.org – Common Function Library Project

IPConvert(ip or numeric version)

Last updated August 28, 2003

author

Aaron Eisenberger

Version: 1 | Requires: CF5 | Library: NetLib

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:

<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:

/**
 * 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;
        }
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson