Is there any way to get a user Country code from a client side (locally)? So far, I have tried the code in this link; https://stackoverflow.com/a/13600004/5947201 but I believe that this code is server side and won't work locally. Thanks
Asked
Active
Viewed 191 times
0
-
1http://stackoverflow.com/questions/17680413/get-visitors-language-country-code-with-javascript-client-side says no, and it says it pretty clearly. Check it out. – LinuxDisciple Mar 25 '16 at 19:10
-
You can get the current position (https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation) and use the coordinates to determine the country – Alon Eitan Mar 25 '16 at 19:10
1 Answers
0
if you don't want to use any web API You can use GeoIP2-php web services and database reader to get users location . you have to download free GeoLite2 databases or any mmdb database can GeoIP2-php read .
Installation (via Phar) - easy way
download phar archive from GeoIP2 Github releases then include it to your project
require 'geoip2.phar';
use GeoIp2\Database\Reader;
$reader = new Reader('GeoLite2-City.mmdb');
$record = $reader->city('128.101.101.101');
print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'

Yassine Qoraiche
- 1,077
- 14
- 32
-
I've tried this but how can I get the country from the IP? I've tried your code and it displayed 'US' but I am not from US.. – Fombe F Mar 25 '16 at 22:23
-