If it can be done in PHP, it can probably be done in Wappler

I am not going to do one of my huge posts for this because I am pretty sure anybody that knows PHP will be able to adapt the concept to what their needs are.

Sometimes I have the need to use something that I already know how to do in PHP very easily, but I either do not have the available option in App Connect or just have not found it.

Take this example, I would like to GeoLocate a client based upon their IP Address, so I create a new page, add my standard App Connect, and Bootstrap 4 CDN, next I add the browser component, and realise there is nothing in there to retrieve the users IP Address, so I add a GeoLocation component thinking it might be in there, and besides Lat and Long coords speed, altitude etc. I see no provision for obtaining the users IP Address.
Obviously I know I could get this data from the Server Connect side, but in this case I really need it in the App Connect side.

This is how
Create an App Connect Variable from Data > Variable
This gives the variable properties of ID, so lets set that to, my_user_ip_address then it gives value, the issue is if you use that value area, it does a dmx-bind:value which will not work in this case, instead I need an actual value and not a bound one.

This is the simplest version of how to get PHP code into App Connect
Add > Data > Variable, then in code view add value=""

<!--ADD PHP TO APP CONNECT VARIABLE-->
<dmx-value id="var1" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"></dmx-value>

<!--CALL THE APP CONNECT VALUE TO DISPLAY IT-->
{{var1.value}}

Lets try it a slightly different way by adding the php into a php variable first

<?php
  $php_var = $_SERVER['REMOTE_ADDR'];
?>

<!--ADD PHP VARIABLE TO APP CONNECT VARIABLE-->
<dmx-value id="var1" value="<?php echo $php_var; ?>"></dmx-value>

<!--CALL THE APP CONNECT VALUE TO DISPLAY IT-->
{{var1.value}}

How about a little more complex, the actual code i use for the https://www.geoplugin.com/webservices/php php API, if it does not work please ensure your php.ini has allow_url_include / allow_url_fopen on

<?php
  function countrychecker($amount) {
    global $geoPlugin_array;

    if (isset($geoPlugin_array['geoplugin_currencyCode']) && $geoPlugin_array['geoplugin_currencyCode'] != 'GBP') { 
      return ($amount * $geoPlugin_array['geoplugin_currencyConverter']);
    }
    return $amount;
  }

  $geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR'] . '&base_currency=GBP') );
?>

<dmx-value id="var_geo_price" value="<?php echo countrychecker(800) ?>"></dmx-value>
<dmx-value id="var_geo_price_symbol" value="<?php echo $geoPlugin_array['geoplugin_currencySymbol'] ?>"></dmx-value>
<dmx-value id="var_geo_price_formatted" dmx-bind:value="var_geo_price.value.toNumber().formatCurrency(var_geo_price_symbol.value, '.', ',', '2')"></dmx-value>
<dmx-value id="var_gb_checker" value="<?php echo $geoPlugin_array['geoplugin_countryName'] ?>"></dmx-value>

Formatted Price {{var_geo_price_formatted.value}}
Currency symbol {{var_geo_price_symbol.value}}
Unformatted Price {{var_geo_price.value}}
Country Name {{var_gb_checker.value}}

<div id="conditional_check_loadedA" is="dmx-if" dmx-bind:condition="var_gb_checker.value != 'United Kingdom'">
  Runs when country is NOT United Kindgom
</div>
<div id="conditional_check_loadedB" is="dmx-if" dmx-bind:condition="var_gb_checker.value == 'United Kingdom'">
  Runs when country is United Kindgom
</div>

Obviously with the concept above in mind I think almost anything can be done.

Lets see what other users and @wappler_ambassadors think about this, and lets hope i do not get in trouble for posting this, it is NOT the normal Wappler way, it is very hacky, probably not recommended, but sometimes when needs must.

3 Likes

that’s why i like wappler even with the shortcoming on some features. if its not there just add it since its PHP and not some weird language

my only complain that you are mixing PHP and JS and you know users can change any client side vars you assign.
it will be bulletproof if it was all on the server.
this is way i requested this feature

Nice workaround and opens up so many possibilities, i have never tried the integration fo php variables into wappler values like that. Perhaps begs the question, should add PHP variable be an option in picker for variables but i guess that goes against the concept of everything visual.
My problem we discussed earlier is i have to take a fixed point and run those coords against 7000 (and growing) other locations and select only those within a very small radius against a worldwide map so for performance reasons it is best done at database query level.

@mrbdrm, I do not doubt what you are saying for a second, but I do love to learn, please can you go onto the page that uses this code here https://www.africacollectionbs4.com and try walk me through how you could go about changing the client side vars. I have never really thought about it to be honest.
This page is still under development so i know it is not anywhere near perfect yet.

@Hyperbytes, yes maybe for that many records it might have a huge performance impact, for other more simple things i think as you say this could open some doors to getting other strange things working.

the simplest way is the console

Sorry but i am probably being really silly here, but what affect does it have, or could it possibly have if a single user changed the value of a variable. As an example, in this case I have two conditionals, lets say i wanted to change the value of the variable so I could potentially see what a UK person was seeing.

So in the console i try
var_gb_checker.value=“United Kingdom”

I was expecting the page information to alter and the condition to then show the page content reserved for United Kingdom but it still does not show.
I mean in inspector I suppose any user could inspect any page on the entire internet and change every paragraph if they wanted to their own text, but if they refreshed the page it would all be back, so I do not see what security impact this has.

Sorry, please try help me understand this better.

the value was set after you have set it with the PHP code. so for example if you post this value to the server you will not get the value from the PHP. and even worse if you are doing any kind of formatting on the page later on it will evaluate to the value i have set and so on.

Ok, i think i understand, so lets say this page loaded, and then i went to console and changed the value of a variable, then if the page had a form that sent an email and database instructions, if I am getting the value for my email or database action from the value in the variable, then because the user altered it in the console, it would enter incorrect data.
Have I understood this right.

yes that’s one use case :slightly_smiling_face:

I don’t like hackers, honestly, why do they have to mess with other peoples stuff, bastards. Everyone should just be nice and leave my vars alone. haha

1 Like

i wish they do
but if that the case then we wouldn’t have PHP in the first place i guess :sweat_smile:

2 Likes

hello everybody this can be done server side with wappler.

1 Like

Thanks @AdrianoLuiz, I do know some of the parts could be done through just the server connect, but it was really just an example of a single thing.

If you could get the last part of this to work with the integration to that geoPlugin system, I would be oh so happy, in reality I would rather never have to resort to anything hacky like this and rather only use server connect and app connect.

To be very honest though I had spent 2 days trying to get that last piece of code to work with only server connect and app connect and just could not seem to get it right.

Please see if you could try recreate that sort of idea, and i will change my code right away to rather use it.

Well I have just tried for another hour to reproduce the results of the final example usage and honestly I must be missing something because with the available components in Server Connect and App Connect alone I can not get it to work.
To be honest nor do I really expect it to work, I mean the sheer amount of API code snippets out there and all the different ways each provider is handling their interfacing I could hardly imagine how this could really be accounted for.
Wappler already provides a way to get a JSON data source which I think most API interfaces would work with anyway, but as for the rest of the strange ones out there I do not really see a way to make it work.

All I can think you were taking about @AdrianoLuiz was only the first 2 examples, which were really only examples and i had already stated

I am hoping I am incorrect though and maybe there is actually a way through Server Connect by adding the parameters to an array variable or something, and replacing the if isset with a condition then running the return in a repeat. But I have tried and tried to no avail.
@Teodor and @George, am I really just being an idiot and not understanding properly, I know that App Connect and Server Connect are far more powerful than what I have managed to work to now, so maybe you could guide me a little.

Hi Paul,

Geo targeting is one of the most advanced things that you can do on a web site.

You really have to ask yourself if it is worth the trouble.

You are slowing down your site enormously by huge queries to locate the users their country or use extended plugins that are also very slow to do that. Most of the plugins you are using are limited in rate anyway so you will have to pay also big time if your site gets more visitors.

Although it might seems like a nice technical challenge to get this done - it seems to me currently a bit our of the Wappler regular users scope.

A much better simple solution is just to redirect the user based on their browser language for internationalization.

Maybe we should find more daily challenges that the regular Wappler users struggle with and not search for the extreme :slight_smile:

1 Like

Lol, sorry George, I know my strange things are always super odd, and I know most users do not have a need for some of my odd ideas, haha.

It actually started out so very simple, the customer signed up for multiple phone numbers and asked for each telephone number to only be displayed to a person in the correct country, I thought I could just use the GeoLocate Component in Wappler and pass the Longatude and Latatude into Google Maps to return the country, then with that information I would be able to switch the currency on the fly.
The problem I have is that I firstly could not get that to work with the currency switch, then I could not get the exchange rate, so it just went from bad to worse.

The really terrible thing is that there are already 2 other elements on my site getting Geo Information, Google Analytics I would assume as well as the LiveChat Plugin being used so now I have added a third tool also doing the same, wish I could have just stolen the info from either of those sources to not add a custom third one.

Oh well, I suppose for the time being at least it works, even if it is not the most elegant solution ever thought up.

Wouldn’t it be a lot easier to show a “select country” dialog with a dropdown on first load? Store the dropdown value in a cookie and use it to change currency or whatever you need.

3 Likes

Sometimes we get lost in all our too extensive technical knowledge and only think of making way to complicated solutions.

The most brilliant solutions are the most simplest ones. That is why I follow the KISS principle :slight_smile: - Keep it Simple Stupid :slight_smile:

And seems Teodor is following it too :slight_smile:

5 Likes

I was originally going to do exactly that to be honest, I actually put it all in place, but got very stuck with the concept of how to get the exchange rate information.
While searching for that solution, i found all these other things and just became a bit of a greedy guts wanting all these amazing things, without really thinking what the implications were going to be.
I can take anything that would normally be a simple 5 minutes and turn it into a 3 week problem.
My daughter asked me to make her a dressing table, and that landed up as a one month, plaster walls, new floor, new lights, and a desk. So that is just my nature i suppose :slight_smile:

2 Likes

i use cloudflare geo location its free and all you have to do is use $_SERVER["HTTP_CF_IPCOUNTRY"];
and it doesn’t slow my site enormously or anything :sweat_smile:
and don’t use google maps if you don’t have to. their new pricing is horrible

2 Likes