Client IP Address debacle! Help please

I need help, please. How can I associate the IP address in this context to dmx.value or at least use the result in an HTML input field. I don’t speak a single word in javascript.

< script type=“application/javascript” >
function getIP(json) {
document.write("My public IP address is: ", json.ip);
}
</ script>

< script type=“application/javascript” src=“https://api.ipify.org?format=jsonp&callback=getIP” >< / script>

Maybe this will help:

Hi @george. I actually have the API function running to pick IP address. However, it passes me my own server IP address. This solution is to retrieve client IP address, pass it to an API function that will retrieve everything else I need from the client IP address.

So I get this: apiurl.php?mykey&ipaddress

This function does retrieve client IP address, I just don’t know how to put it in a form input field to pass it to the API.

Thanks in advance for every help.

How critical is this function? Fetching the IP address client-side is not a very wise idea from a security standpoint, as the user could manipulate the client-side code to return a different address. The problem here is you’re getting the server’s IP address instead of the client’s. A properly configured web server would be smart to pass the client’s IP address to your app

How are you hosting your website? Is it PHP or NodeJS? Are you using a load balancer in front? CloudFlare?

Hi @Apple. Actually, in my application, even a VPN connection is not a problem, but abstractapi.com also determines VPN connections. I just want a simplified form that captures vital geo and IP variables which the user can change later. Clunky it might seem, but if it is programmatically achievable, I’m going for it.

However, that function shoots the client IP, how to get it off javascript into an HTML input field is what beats the S*** out of me. I know it’s simple, I just don’t know-how. **sigh

You can use dmx.parse within a script to set the value of a dmx variable.

For example

< script type=“application/javascript” >
function getIP(json) {
document.write("My public IP address is: ", json.ip);
dmx.parse.var1.setValue(json.ip);
}
</ script>

You could also set the value of an input directly.

Hi @mebeingken,
Shoots this error:

Uncaught TypeError: Cannot read properties of undefined (reading ‘setValue’)
at getIP (register.html:106:52)
at api.ipify.org/:1:1

and Value is not accessible at {{var1.value}}

Sorry, that was totally wrong. Try this:

dmx.parse("var1.setValue('" + json.ip + "')");

Hi again @mebeingken,
It still doesn’t render in {{ var1.value }} or {{ var1 }}

If the variable is in a content page, then this:

dmx.parse("content.var1.setValue('" + json.ip + "')");

I have this going:

function getIP(json) {
document.write(json.ip);
dmx.parse("content.var1.setValue('" + json.ip + "')");

                                }

Still inaccessible.

If json.ip is the value from the script, and var1 is a variable on a content page, then the above looks correct to me. Use the dev console to troubleshoot. Put a console.log(json.ip) in there to make sure it is evaluating, etc.

It echo's in console (Ignore the errors)

ypeError: Cannot read properties of undefined (reading 'get')
at t (parser.js:695:1)
at parser.js:780:1
at parser.js:740:1
at parser.js:495:1
at Object.dmx.parse (parser.js:428:1)
at getIP (register.html:106:41)
at ?format=jsonp&callback=getIP:1:1
dmx.parse @ parser.js:430
getIP @ register.html:106
(anonymous) @ ?format=jsonp&callback=getIP:1

**register.html:107 94.205.33.7**

routes.js:1 Uncaught TypeError: Cannot set properties of undefined (setting 'router')
at routes.js:1:20

Does this actually works?

dmx.parse("content.var1.setValue('1.2.3.4')");

Run that in dev console

Good morning @Apple. Sorry responding late. I hit snooze. I just ran this on dev console and here is the result.

dmx.parse(“content.var1.setValue(‘1.2.3.4’)”);
undefined
dmx.parse(“content.var1.setValue(‘1.2.3.4’)”);
undefined

No problem responding late! Hope you had a good sleep :slight_smile:

Do you have a way to check if “var1” was updated on the website? Are you binding “var1” to a form field? (dmx-bind)

dmx.parse returning undefined it’s not a red flag me yet

(worth mentioning I’m going to sleep soon)

Using dmx-bind:value=“var1”
Still does not render. Can I pick the value from console? Do you know how?

I would use a global:

< script type=“application/javascript” >
function getIP(json) {
document.write("My public IP address is: ", json.ip);
dmx.global.set('ipadd',json.ip);
}
</ script>

You can then use it in data bindings such as:

<input type="text" dmx-bind:value="ipadd">

When are you calling the getIP function? When the API request completes? Do you see the ‘My public ip is’ text on your page?

IGNORE the question about when - I can see it’s a callback from the script

2 Likes

Standing on my feet, looking around at everyone, looking at you, nodding my head, smiling so big, and clapping so hard my palm hurts!

Well done @bpj! This worked.
Thank you all for helping me through this. I really appreciate the help and your time.

This is the solution for client-side IP Address capture using external API.

giphy

Like this. :slight_smile:

2 Likes