Skip to main content
Participant
November 10, 2009
Question

Restricting a page based on IP range

  • November 10, 2009
  • 6 replies
  • 3949 views

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 has been closed for replies.

    6 replies

    Known Participant
    August 16, 2019

    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.

    WolfShade
    Legend
    August 16, 2019

    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,

    ^ _ ^

    Charlie Arehart
    Community Expert
    Community Expert
    August 16, 2019

    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.

    /Charlie (troubleshooter, carehart. org)
    Inspiring
    November 11, 2009

    Hi Scotch123,

    I think this UDF might help you to acheive that.

    http://cflib.org/udf/isIPInRange

    Inspiring
    November 11, 2009

    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

    scotch123Author
    Participant
    November 10, 2009

    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!

    Known Participant
    November 10, 2009

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

    Inspiring
    November 10, 2009

    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)

    ilssac
    Inspiring
    November 10, 2009

    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.