Obtain Client IP & Store To DB

Is there a Wappler way, i.e. use dmx-browser, rather than a PHP script to get client ip and url so they can be stored to database?

Here’s what I have found and currently using:

<?php
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
    $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    else if (!empty($_SERVER["HTTP_X_FORWARD"])) {
    $ip = $_SERVER["HTTP_X_FORWARD"];
    }
    else {
    $ip = $_SERVER["REMOTE_ADDR"];
    }
?>

Then in the insert form I have this:

<!-- hidden -->
<input type="hidden" name="ip_address" value="<?php echo $ip; ?>" />
<input type="hidden" name="url_address" dmx-bind:value="browser1.location.origin + '' + browser1.location.pathname">
<!-- hidden -->

This works okay. However, instead of using PHP code, I would prefer to use a dmx-bind:value with a browser component that retrieves the IP address of the client.

Is such a thing available?

doubtful about a Wappler native way to get IP from client side.

this is what we did to get IP from client side in a PHP Wappler SPA:

  1. a tiny JS function
  2. a ServerAction to set client ip as a server side session variable to be reused elsewhere in other ServerActions.
  3. A ServerConnect on the index page to call that ServerAction with a get param of the ip as returned by the JS function.

JS Function (uses ipify.org free service to get IP address on client side)

function GetIP() {
    $.getJSON("https://api.ipify.org/?format=json", function (e) {
        dmx.parse("ServerConnect.load({client_side_ip: '" + e.ip + "'})");
    });
}

we initialised this JS function on click on the button that opens up the login modal, so we can ensure that we have the client ip available for all other server actions just before login:

<button id="btnLoginModal" class="btn btn-primary" onclick="GetIP();">
1 Like

Why are you trying to do this on the client side?

If you want to store to a DB, you are best doing this in a SC API action. You could simply use a setValue step with a value something like:

$_SERVER.HTTP_CLIENT_IP.default($_SERVER.HTTP_X_FORWARDED_FOR.default($_SERVER.REMOTE_ADDR))
2 Likes

I guess it’s because I’m an old man noob. Ha!
Nevertheless, I can learn a lot from you young guys. Not too proud to ask for help. Ha!

2 Likes

Also would to add that we could not use server var for IP coz we’re using load balancer - so the IP returned by server variable is that of the load balancer server.

Hence the long way to get IP from client side and then pass that to server side.

1 Like

Thank you both very much!

Now, I shall go back and update the login pages so that they reflect what I’ve learned today from you.