4 Replies Latest reply on Feb 14, 2007 10:05 AM by Newsgroup_User

    Using an IP range as a conditional

    Level 7
      I would like to show one set of links to our users who are inside our
      network and a different set to those outside our network. We have an IP
      range from 192.168.0.0 - 192.168.255.255. I am capturing the IP address of
      the visitor using CGI.REMOTE_ADDR.

      How can I compare the captured IP address to evaluate if it is GTE
      192.168.0.0 AND LTE 192.168.255.255?


        • 1. Re: Using an IP range as a conditional
          Level 7
          Just break the ip into its parts

          <cfset testIP = "192.168.125.12">

          <cfif listGetAt(testIP,1) EQ 192 AND
          listGetAt(testIP,2) EQ 168 AND
          (listGetAt(testIP,3) GTE 0 AND listGetAt(testIP,3) LTE 255) AND
          (listGetAt(testIP,4) GTE 0 AND listGetAt(testIP,4) LTE 255)>
          Do whatever
          <cfelse>
          Do something else
          </cfif>

          For your particular example you really only need to check that the first
          two parts equal 192 and 168, but this provides the full functionality.
          • 2. Re: Using an IP range as a conditional
            Level 7
            Oops, didn't provide the proper syntax for the listGetAt function.

            You need to use this form: listGetAt(testIP,1,".")
            • 3. Re: Using an IP range as a conditional
              Dan Bracuk Level 5
              <cfif theip gte "192.168.0.0" and theip lte "192.168.255.255">

              Plus some other code in case the user is not providing their ip address.
              • 4. Re: Using an IP range as a conditional
                Level 7
                On Wed, 14 Feb 2007 12:16:49 -0500, Wally Kolcz wrote:

                > I would like to show one set of links to our users who are inside our
                > network and a different set to those outside our network. We have an IP
                > range from 192.168.0.0 - 192.168.255.255. I am capturing the IP address of
                > the visitor using CGI.REMOTE_ADDR.
                >
                > How can I compare the captured IP address to evaluate if it is GTE
                > 192.168.0.0 AND LTE 192.168.255.255?

                if (reFind("192\.168\.[0-9]{1,3}\.[0-9]{1,3}", CGI.REMOTE_ADDR)

                That regex will not an IP address, per-se, as it'll match stuff like
                192.168.999.999, but the web server will only pass valid IP addies, so that
                part of the validation is already done.

                This'll only work for complete 0-255 0-255 ranges on the last two bytes,
                but that meets your stated requirement.

                --
                Adam