Skip to main content
Siam Parvez
Participant
June 18, 2022
Answered

How to get the user ip address?

  • June 18, 2022
  • 2 replies
  • 2073 views

Is there any variable in coldfusion that stores user ip address?

Please let me know.

Thank you

This topic has been closed for replies.
Correct answer BKBK

 

I was trying to get my IP from an API (ipify.org), from here I can get the data in JSON format. Can you tell me how I can convert that into a coldfusion variable?


By @Siam Parvez

 

Yes.

Here's how:

 

<cfhttp url="https://api.ipify.org?format=json" />

<!--- Conversion: JSON string response to a struct --->
<cfset content=deserializeJSON(cfhttp.filecontent)>

<cfset IP = content.ip>

 

2 replies

Charlie Arehart
Community Expert
Community Expert
June 26, 2022

Siam, did you ultimately get the solution you needed? Can you confirm which solved your particular need? 

/Charlie (troubleshooter, carehart. org)
Charlie Arehart
Community Expert
Community Expert
June 18, 2022

Dump the cgi scope. Lots of cool stuff there, including remote_addr with the ip. And if ever that's not the RIGHT one, it may be that of some proxy or load balancer. In that case, the correct ip should be in a header, available from gethttprequestdata(). 

/Charlie (troubleshooter, carehart. org)
Siam Parvez
Participant
June 18, 2022

Hey Charlie !

Thanks for the reply. I have heard alot about you from Mr. Larry J. Morrow, A-PLUS.

I tried both but didn't find the IP address.

I was trying to get my IP from an API (ipify.org), from here I can get the data in JSON format. Can you tell me how I can convert that into a coldfusion variable?

Thank you.

Charlie Arehart
Community Expert
Community Expert
June 18, 2022

Thanks for the kind regards, and to Larry as well. As for saying you  "didn't find the IP address", can you clarify? You didn't find any? Or you didn't find what you KNOW the user's ip to be? And let's be clear, you said you want to find the "user ip address", so I assume you mean the user visiting some cf site of yours, right?

 

If so, you now say you want to "get my IP". You, as just another person visiting the cf page?

 

Or do you mean you as the server SERVING the cf page? That seems perhaps the case, since you refer to ipify.org, which is for doing that job (it returns the ip address of whoever calls that). To be clear, you CAN just call that, using cfhttp. Have you tried it?

 

If you might wonder how to get your ip without calling out, and no other result in the cgi scope is correct, I am not recalling now any cf function or other variable that reports it. Try dumping the server scope. (I'm writing from a phone.)

 

There are also various Java classes and methods folks have turned to over the years, which you can call from cfml, like CreateObject("java", "java.net.InetAddress"). But that too may not report what you expect, so calling out like that to ipify may be your best bet. 

/Charlie (troubleshooter, carehart. org)