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.
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:
a tiny JS function
a ServerAction to set client ip as a server side session variable to be reused elsewhere in other ServerActions.
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:
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.