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

How to get the user ip address?

New Here ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

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

Please let me know.

Thank you

TOPICS
Advanced techniques

Views

1.0K

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

correct answers 1 Correct answer

Community Expert , Jun 19, 2022 Jun 19, 2022

 

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>

 

Votes

Translate

Translate
Community Expert ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

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)

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
New Here ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

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.

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
Community Expert ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

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)

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
Community Expert ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

 

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>

 

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
Community Expert ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

LATEST

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


/Charlie (troubleshooter, carehart.org)

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