If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

It was asked on CFTalk the other day about how to access the header that most load balancers populate with the real IP address of a user. Due to the way that load balancers work the actual request comes from the LB and not the user themselves. To be able to know what the IP address of the user is the LB will populate an HTTP Header called "X-Forwarded-For" while the cgi.remote_addr will be the IP address of the LB. To read this information we need to use GetHttpRequestData() which will tell us what is in the header. From there we can alter the CGI so the rest of the application is aware of the correct IP address.

Here is a sample piece of code...

CFM:
  1. <!--- Make sure that we know the users real ip address --->
  2. <cftry>
  3. <cfif StructKeyExists(GetHttpRequestData().headers, "X-Forwarded-For")>
  4.        <cfset request.remote_addr = GetHttpRequestData().headers["X-Forwarded-For"]>
  5. <cfelse>
  6.        <cfset request.remote_addr = CGI.REMOTE_ADDR>
  7. </cfif>
  8. <cfcatch type="any"></cfcatch>
  9. </cftry>

Hope this helps!

Comments

Wilgeno on 3 March, 2008 at 11:15 am #

The load balancer we use passes “cgi.HTTP_X_CLIENTSIDE”. We’ve used essentially same code for the past year.


Post a Comment
Name:
Email:
Website:
Comments: