We help IT Professionals succeed at work.

Cfparam and Findnocase in Coldfusion

dsk1234
dsk1234 asked
on
Hi All,

We have the  following code in coldfusion that will accept single Ip address.

//Setting single IP Address

<cfparam name="IPAddress" default="xx.x.x.xxx">  


//Checking condition


<cfif cgi.server_port_secure neq 1 and FindNoCase("#IPAddress#", cgi.remote_addr, 1) neq 1>
do something

<cfelse>

do something

</cfif>

I need to  modify the above code with set of multiple static IP addresses in cfparam and checking those IPAddresses in FindNocase? How can I achieve this?

Thanks in Advance
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
If need an exact match, I would use list functions.  

     <!--- set the list of static ip addresses to check for --->
     <cfset ipAddressList = "127.0.0.1,192.168.1.1,....etc....">
     <cfif listFind(ipAddressList, cgi.remote_addr)>
           found cgi.remote_addr in list ...
     <cfelse>
           not found. do something else
     </cfif>

> <cfparam name="IPAddress" default="xx.x.x.xxx">  

Unless you need to pass the value from another page, I'd just use CFSET instead.  Because using cfparam, someone could override the value by adding a new value onto the URL.

       http://somesite.com/yourPage.cfm?ipaddress=xx.xxx.xxx.xxx

Author

Commented:
Thanks for the quick response and will get back to you.

Author

Commented:
Hi,

we have another 2  pages where we are referring the IPAddress(es) with the following code

<cfscript>
function f1()
{
      if(FindNoCase(#IPAddress#, cgi.remote_addr, 1) eq 1)
      {
          do something
            
      }
      else
      {
      do something      
      }
      
}
</cfscript>

So I believe we need to change the above code  with listFind as per your suggestion to accomodate multiple static Ip addresses.Is it correct?

Since we are referring the IPAddresses in another 2 pages, do we still need to use cfset while assigning multple static IP addresses ?
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
> we have another 2  pages where we are referring the IPAddress(es) with the following code
> So I believe we need to change the above code  with listFind

Yes

> Since we are referring the IPAddresses in another 2 pages, do we still need to
> use cfset while assigning multple static IP addresses ?

Not sure I understand the question.  Are you using the same list of multiple IP addresses on all 3 pages?
CERTIFIED EXPERT
Commented:
You should probably set up your list of IP addresses in a central locaton;  application.cfc / .cfm file and then use a request scope to make them global to your pages..

<cfset request.adminIPaddresses = ".... list of IPs....">

CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
Yeah, if it's the same the list (I wasn't quite clear on the setup).  I was thinking if the function does the same thing in all 3 pages, it should also be stored in a central location. So you don't have the same code repeated in multiple places.

Author

Commented:
At present the following  code will accecpt single static IP address and based on this IP address we are checking condition and redirecting to specific Url.
 
I was told that the application should now handle multple static IP addresses.So I searched for the IPAddress instance  in the application and found in the follwing 3 places.

The following code should be changed accordingly to handle multiple static Ip addresses.

Existing code

1)app.cfm  //Global settings page

<cfparam name="IPAddress" default="xx.x.x.xxx"> //single IP Address

<cfif cgi.server_port_secure neq 1 and FindNoCase("#IPAddress#", cgi.remote_addr, 1) neq 1>  //Checking condition

do something

<cfelse>

do something

</cfif>


2)RefPage1.cfm

<cfscript>
function f1()
{
      if(FindNoCase(#IPAddress#, cgi.remote_addr, 1) eq 1)
      {
          do something
            
      }
      else
      {
      do something      
      }
      
}
</cfscript>

3)RefPage2.cfm

<cfscript>
function f1()
{
      if(FindNoCase(#IPAddress#, cgi.remote_addr, 1) eq 1)
      {
          do something
            
      }
      else
      {
      do something      
      }
      
}
</cfscript>
CERTIFIED EXPERT
Commented:
I suggest centralizing your code completely.

In your application.cfc file, test the IP address to see if it matches one of the three, if it does, set a global variable.  Then test this global variable ONLY in all the other pages..

<cfset request.isAdministrator = false>
<cfif listFind("... IP address List....", cgi.remote_addr)>
     <cfset request.isAdministrator = true>
</cfif>


Then on all other pages, simply test the the variable...

<cfif request.isAdministrator>
    ... do something
<cfelse>
   .. do something else
</cfif>

CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:

> <cfif listFind("... IP address List....", cgi.remote_addr)>

I agree with the above suggestions. Just remember it's very important NOT to use <cfparam> to declare the list of ip addresses.  Use cfset instead. Otherwise, a user could pass in whatever value they want through the url. Then they'd be declared an Administrator and have access to things they shouldn't...

Author

Commented:
Thank you guys.Will get back to you on the above approach.

Author

Commented:
Thank you guys.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.