CFLib.org – Common Function Library Project

isIPInRange(sIP, sIPREList)

Last updated April 14, 2005

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

 
Rated 3 time(s). Average Rating: 3.7

Description:
Returns true if the passed IP address matchs any of the IP addresses or IP address ranges supplied in a Regex list. This code be to useful in blocking or granting people access based on location. I used it to block people running check/cheque scams from certain areas of the website. Must use comma delimited list.

Return Values:
Returns a boolean.

Example:

view plain print about
<CFSET blockedIPrange="^217\.78\.(6[4-9]|7[0-9])\.[0-9]+,^195\.166\.2(2[4-9]|[3-4][0-9]|5[0-5])\.[0-9]+">

<cfif isIPInRange(CGI.REMOTE_ADDR,blockedIPrange)>
This website is not available in your location.
<cfabort>
</cfif>

Parameters:

Name Description Required
sIP The IP. Yes
sIPREList List of IP Regex strings. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Is this IP within any of the IP ranges supplied.
 * 
 * @param sIP      The IP. (Required)
 * @param sIPREList      List of IP Regex strings. (Required)
 * @return Returns a boolean. 
 * @author Peter Crowley (pcrowley@webzone.ie) 
 * @version 1, April 14, 2005 
 */

function isIPInRange(sIP,sIPREList) {
    var i = 1;
    var nREListCount=ListLen(sIPREList);
    
    for (i = 1; i LTE nREListCount; i = i+1) {
        if (REFind(ListGetAt(sIPREList,i),sIP)) return true;
    }
    return false;
}
</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
16 day(s) ago

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

Ben Forta Ben Forta added
GetHostAddress
29 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