Forked from ghidinelli/gist:e01b83b6a2e628c17cd5
Last active
September 14, 2015 14:05
-
-
Save mjclemente/31abdfe8ac97c43940ed to your computer and use it in GitHub Desktop.
getRemoteAddress() for ColdFusion with or without common load balancers/firewalls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="getRemoteAddress" output="false" access="public" returntype="string" hint="Identify the remote user IP address"> | |
<cfset var pc = getHTTPRequestData().headers /> | |
<cfset var arrIP = "" /> | |
<cfif structKeyExists(pc, "X-Forwarded-For") AND len(pc["X-Forwarded-For"])> | |
<!--- the x-forwarded-for header sometimes includes values that are too long like "172.27.156.64, 67.98.222.16". The regexp picks out just the matches. http://support.f5.com/kb/en-us/solutions/public/12000/200/sol12264.html ---> | |
<cfset arrIP = reMatch('\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b', pc["X-Forwarded-For"]) /> | |
<cfif arrayLen(arrIP)> | |
<cfreturn arrIP[1] /> | |
<cfelse> | |
<cfreturn CGI.remote_addr /> | |
</cfif> | |
<cfelse> | |
<cfreturn CGI.remote_addr /> | |
</cfif> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment