How do I identify the country the browser is in?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
How do I identify the country the browser is in?
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?";}
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?";}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I identify the country the browser is in?
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.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I identify the country the browser is in?
Thanks for that.
I did think it would be feasible via the IP.
That first one:
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:
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".
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"
}
}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... */
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".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I identify the country the browser is in?
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"....
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"....
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I identify the country the browser is in?
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.
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?
That's exactly right.simonmlewis wrote: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".
Fire it when the DOM loads. (ie. $(document).ready())simonmlewis wrote:It doesn't however explain how to trigger it when the page loads.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I identify the country the browser is in?
I'm asking my hosting company about it as I don't know how to install that.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.
How? Sorry I'm brand new to this particular stuff.Fire it when the DOM loads. (ie. $(document).ready())
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I identify the country the browser is in?
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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I identify the country the browser is in?
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:I'm asking my hosting company about it as I don't know how to install that.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.
Code: Select all
php composer.phar installSurely you have some JS that loads whenever your site loads. You can place it in there.simonmlewis wrote:How? Sorry I'm brand new to this particular stuff.Fire it when the DOM loads. (ie. $(document).ready())
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
});
}
})-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I identify the country the browser is in?
Code: Select all
php composer.phar installI 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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I identify the country the browser is in?
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:This is the bit I'm going to have trouble with.Code: Select all
php composer.phar install
I take it this is done via Putty?
Sadly no. Even IPs that look very close numerically can be widely disparate geographically.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!
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I identify the country the browser is in?
How do I install composer locally then?You'll need to do it locally to test things out anyway
I assume through the CMD prompt, but how?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I identify the country the browser is in?
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:
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?!?!
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);
?>[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?!?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I identify the country the browser is in?
You've required the Composer autoloader? If so, have you made sure to require it prior to loading testcountry.inc?