Page 1 of 3
How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 3:23 am
by simonmlewis
We need to spot if someone is looking at a site from the USA, and then guide them to our US web site.
I did find some forum threads on this, but they go back to 2008, and I suspect there are now better ways of doing it thru PHP.
I thought it would be via the IP, but maybe it's using some $_SERVER method??
Once identified, I need to be able to query on the result. ie. if ($country == "usa") { echo "did you mean to go to this site instead?";}
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 6:56 am
by Celauran
This article does a pretty good job of explaining it. As the author mentions, using IP address isn't super reliable but should be sufficient for the use case you've described. Alternately, you can ask the user for their location using JavaScript as described in
this article. That's going to be more reliable, but the user has the option of saying no.
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 7:18 am
by simonmlewis
Thanks for that.
I did think it would be feasible via the IP.
That first one:
Installation of Geocoder is most easily done using composer. Add the following to your composer.json:
Code: Select all
{
"require": {
"willdurand/geocoder": "@stable"
}
}
I've no idea why my composer.json file would be, or if I ever have one.
Following on in that article, I saw this code, that's meant to display (I assume) the information they show on the page:
Code: Select all
$adapter = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$geocoder = new \Geocoder\Geocoder();
try {
$result = $geocoder->registerProvider( new \Geocoder\Provider\FreeGeoIpProvider($adapter) )
->geocode($_SERVER['REMOTE_ADDR']);
} catch (Exception $e) {
$result = $e->getMessage();
}
/* And so on for all the other providers as well... */
But when I run this locally, it obviously throws a fit because I don't have things installed.
So how do I get that composer file installed, and where?
This would be purely to spot if the person's browser is in the USA.
Then I will need to do the same for Ireland or "Northern Ireland".
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 7:52 am
by simonmlewis
I'm looking at this:
https://developer.mozilla.org/en-US/doc ... eolocation
I suspect this is the type you get when you are asked "Share Location".
Not sure I might this, but others might.
It doesn't however explain how to trigger it when the page loads.
So both options look viable, but for me, with maybe less experience, it doesn't say "how to install"....
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 7:53 am
by Celauran
Start here:
https://getcomposer.org/
tl;dr
Your composer.json is typically in the project root and installs packages to /vendor. Specify the packages you need in composer.json, run composer install, and call the composer autoloader when bootstrapping your application.
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 7:56 am
by Celauran
That's exactly right.
simonmlewis wrote:It doesn't however explain how to trigger it when the page loads.
Fire it when the DOM loads. (ie. $(document).ready())
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 8:03 am
by simonmlewis
Your composer.json is typically in the project root and installs packages to /vendor. Specify the packages you need in composer.json, run composer install, and call the composer autoloader when bootstrapping your application.
I'm asking my hosting company about it as I don't know how to install that.
Fire it when the DOM loads. (ie. $(document).ready())
How? Sorry I'm brand new to this particular stuff.
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 8:10 am
by simonmlewis
Having seen how Amazon.com do it, when I visit that from here in the UK, we don't want the user to be asked to confirm their location. It just needs to know.. and action. So the composer.json requirement is obviously high. Apart from that, what else do I need to do... I'm looking at that page, but it's a little bit greek to me!
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 8:15 am
by Celauran
simonmlewis wrote:Your composer.json is typically in the project root and installs packages to /vendor. Specify the packages you need in composer.json, run composer install, and call the composer autoloader when bootstrapping your application.
I'm asking my hosting company about it as I don't know how to install that.
It's reasonably well explained on the Composer site. The short of it is you download composer.phar, place it in your project directory, and call it from the command line.
simonmlewis wrote:Fire it when the DOM loads. (ie. $(document).ready())
How? Sorry I'm brand new to this particular stuff.
Surely you have some JS that loads whenever your site loads. You can place it in there.
Code: Select all
$(document).ready(function() {
if (navigator.geolocation) {
var pos = navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
// do other stuff here
});
}
})
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 8:22 am
by simonmlewis
This is the bit I'm going to have trouble with.
I take it this is done via Putty?
That always scares me. lol. Thankfully the site is on a managed dedicated server, so they may be happy to do it for me.
I honestly thought the first few digits of the IP would be the country..... I really thought it would be as easy as that!
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 8:25 am
by Celauran
simonmlewis wrote:
This is the bit I'm going to have trouble with.
I take it this is done via Putty?
You'll need to do it locally to test things out anyway, so you can always push the whole vendor directory along with your other changes. If you're not deploying via FTP, we can discuss options based on your deployment strategy.
simonmlewis wrote:I honestly thought the first few digits of the IP would be the country..... I really thought it would be as easy as that!
Sadly no. Even IPs that look very close numerically can be widely disparate geographically.
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 8:32 am
by simonmlewis
You'll need to do it locally to test things out anyway
How do I install composer locally then?
I assume through the CMD prompt, but how?
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 8:33 am
by Celauran
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 9:35 am
by simonmlewis
A few developments.
I've installed composer locally. I can see the file in phpmyadmin.
I added that require line.
Then added this to a test page:
Code: Select all
<?php
$lookfor = 'Laan van Meerdervoort, Den Haag, Nederland';
$adapter = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$geocoder = new \Geocoder\Geocoder();
$geocoder->registerProvider(new \Geocoder\Provider\GoogleMapsProvider($adapter));
$result = $geocoder->geocode($lookfor);
?>
I get this:
[text]Fatal error: Class 'Geocoder\HttpAdapter\CurlHttpAdapter' not found in C:\xampp\phpMyAdmin\site\includes\testcountry.inc on line 5[/text]
Maybe I am being too simplistic with it?!?!
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 9:49 am
by Celauran
You've required the Composer autoloader? If so, have you made sure to require it prior to loading testcountry.inc?