• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using an IP range as a conditional

LEGEND ,
Feb 14, 2007 Feb 14, 2007

Copy link to clipboard

Copied

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?


TOPICS
Advanced techniques

Views

357

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2007 Feb 14, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2007 Feb 14, 2007

Copy link to clipboard

Copied

Oops, didn't provide the proper syntax for the listGetAt function.

You need to use this form: listGetAt(testIP,1,".")

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2007 Feb 14, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2007 Feb 14, 2007

Copy link to clipboard

Copied

LATEST
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation