I am trying to change my active currency based on user IP address. it's already working in airbnb
Asked
Active
Viewed 2,288 times
0
-
2Okay. Go ahead! http://stackoverflow.com/a/17864552/1943607 – Reinherd Sep 24 '13 at 06:07
-
possible duplicate of [Getting the location from an IP address](http://stackoverflow.com/questions/409999/getting-the-location-from-an-ip-address) – Baby Groot Sep 24 '13 at 06:25
-
What have you tried... show us the codes... etc... you probably aren't coming back. – Sep 24 '13 at 06:26
1 Answers
0
Call http://ip-api.com/json/ (free IP to Contry service with a rate limit). Get the data and parse it something like:
<?php
$remote_IP_url = 'http://ip-api.com/json/' . $_SERVER['REMOTE_ADDR'];
$remote_user_data = json_decode(file_get_contents($remote_IP_url));
if ( $remote_user_data->status == 'success' ) {
$user_country = $remote_user_data->countryCode;
// do your check and get the currency code w.r.t. the $user_country in the previous line
}
else {
// do your error handling
}
?>
NOTE: This API is free and limited by usage rate. So, when you get an IP address, check against your database entry. If not found, get the data from API and store the JSON data into your database table with IP address, otherwise get the result from the database table that you already stored.
Hope it helps.

anupam
- 756
- 5
- 11
-
-
I don't know if there exists anything that gives us currency against an IP address. You can always build a table with CountryCode and Currency and then get the currency from the table w.r.t. the country code that you got from the API. – anupam Sep 24 '13 at 06:23
-
@user1659887 My service, usercountry.com, gives currency along with other country data against an IP address. – byl83 Aug 10 '17 at 04:15