Skip to main content
Participant
Nov 10, 2009 4:31 PM UTC
Question

Restricting a page based on IP range

Hello,

CF newb here. I know how to restrict access to a page based on individual IPs using:

<cfif TRUE AND CGI.REMOTE_ADDR NEQ '123.45.67.80'
AND #cgi.remote_addr# NEQ '123.45.67.81
AND #cgi.remote_addr# NEQ '123.45.67.84'>

But I need to restrict access to a page to just allowing people in my local IP range to see it. Is there a way of excluding all IP except for those in, say, 123.45.67.80 through 123.45.67.99 without having to key in all of them individually?

Thanks a lot!!

    This topic is closed to new replies. Start a new post to keep the conversation going.

    12 replies

    Known Participant
    Aug 16, 2019 10:52 AM UTC

    This is an really old question, but since it was not answered.

    In Lucee you can just use isIPinRange() https://docs.lucee.org/reference/functions/isipinrange.html

    https://docs.lucee.org/reference/functions/isipinrange.html

    I don't know if this is available in the Adobe version of Coldfusion yet.

    Translate
    WolfShade
    Legend
    Aug 16, 2019 1:04 PM UTC

    XIntelligence  wrote

    I don't know if this is available in the Adobe version of Coldfusion yet.

    It may never be added to ACF.  The Adobe programmers aren't exactly known for considering what us devs want/need.

    V/r,

    ^ _ ^

    Translate
    Charlie Arehart
    Community Expert
    Community Expert
    Aug 16, 2019 7:11 PM UTC

    Wolf, why the snarkiness ? I do love how often you are able to help folks here so often, but why not leave it at that? Why the occasional jabs?

    At least strive to be accurate, rather than fall into the "fake news" trap. There are PLENTY of ways that the CF team has responded to developer wants and needs, with EVERY update and EVERY new version to CF. Do they do everything everyone wants? No. But that's true of all tech (including Lucee).

    But back to the original question, no, CF currently does not offer a function like that discussed above. There are many things in Lucee that are not in CF, just like there are many things in CF that are not in Lucee. Each team decides (and demand from their user base influences) what features are worth adding--and both sometimes add what the other had added, even without demand, if it's deemed simple and useful enough.

    But as for those seeking this function, if you do want it in CF, you can file a feature request at tracker.adobe.com. And yes, the CF team really does respond to those. No, again, not EVERY request or bug, but MANY with each update and new version. Really.

    Translate
    /Charlie (troubleshooter, carehart. org)
    Inspiring
    Nov 11, 2009 4:52 AM UTC

    Hi Scotch123,

    I think this UDF might help you to acheive that.

    http://cflib.org/udf/isIPInRange

    Translate
    Inspiring
    Nov 11, 2009 3:18 AM UTC

    convert start & end of IP range to decimal, then simply check if the IP is

    within that range (this is how the db based geolocators work).

    ]]>

    function ipNumber(ipAddress) {

    var a=listFirst(arguments.ipAddress,".");

    var b=listGetAt(arguments.ipAddress,2,".");

    var c=listGetAt(arguments.ipAddress,3,".");

    var d=listLast(arguments.ipAddress,".");

    return (a * 16777216) + (b * 65536) + (c * 256) + d;

    }

    </cfscript

    Translate
    scotch123Author
    Participant
    Nov 10, 2009 6:24 PM UTC

    Thanks a lot, guys! Much appreciated. I believe I was able to do it via IIS by excluding all IP except for our local range, but the CF snippets will come in very handy, too.

    Cheers!

    Translate
    Known Participant
    Nov 10, 2009 5:12 PM UTC

    <cfset ipaddress = cgi.remote_addr>

    <cfset classa = listgetat(ipaddress,1,".")>

    <cfset classb = listgetat(ipaddress,2,".")>

    <cfset classc = listgetat(ipaddress,3,".")>

    <cfset classd = listgetat(ipaddress,4,".")>

    <cfif classa eq 123 and classb eq 45 and classc eq 67 and classd lte 99 and classd gte 80>

         <!--- whatever you want to do --->

    </cfif>

    Translate
    Inspiring
    Nov 10, 2009 4:59 PM UTC

    if (left(cgi.remote_addr, 9) is "123.45.67"

    and right(cgi.remote_addr, 2) gte 80

    and right(cgi.remote_addr, 2) lte 99)

    Translate
    ilssac
    Inspiring
    Nov 10, 2009 5:05 PM UTC

    You might also want to look at the IP address as a dot {.} deliminited list and see what you can do with the list functions.

    http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_13.html

    Also, you may want to think outside of the reactor (my ColdFusion metaphore for a box).  And see about what you can do within the Web Server.  All the major web servers have the ability to restrict requests to specific content to specific IP addresses or address ranges.

    Translate