How do I identify the country the browser is in?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

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?

Post by simonmlewis »

With vardump:
[text]Arrayarray(18) { ["geoplugin_request"]=> string(13) "79.68.120.150" ["geoplugin_status"]=> int(206) ["geoplugin_credit"]=> string(145) "Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com. ["geoplugin_city"]=> string(0) "" ["geoplugin_region"]=> string(0) "" ["geoplugin_areaCode"]=> string(1) "0" ["geoplugin_dmaCode"]=> string(1) "0" ["geoplugin_countryCode"]=> string(2) "GB" ["geoplugin_countryName"]=> string(14) "United Kingdom" ["geoplugin_continentCode"]=> string(2) "EU" ["geoplugin_latitude"]=> string(4) "51.5" ["geoplugin_longitude"]=> string(5) "-0.13" ["geoplugin_regionCode"]=> string(0) "" ["geoplugin_regionName"]=> NULL ["geoplugin_currencyCode"]=> string(3) "GBP" ["geoplugin_currencySymbol"]=> string(6) "£" ["geoplugin_currencySymbol_UTF8"]=> string(2) "£" ["geoplugin_currencyConverter"]=> string(6) "0.6573" }
[/text]
Without:
[text]array[/text]

Code: Select all

<?php
$varname = (unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
$varname['geoplugin_countryCode'];
echo "$varname";

?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I identify the country the browser is in?

Post by Celauran »

Code: Select all

[17:02][local][~]
% php -a
Interactive shell

php > $var = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=xxx.xxx.xxx.xxx'));
php > var_dump($var);
array(18) {
  'geoplugin_request' =>
  string(15) "xxx.xxx.xxx.xxx"
  'geoplugin_status' =>
  int(200)
  'geoplugin_credit' =>
  string(145) "Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>."
  'geoplugin_city' =>
  string(0) ""
  'geoplugin_region' =>
  string(2) "QC"
  'geoplugin_areaCode' =>
  string(1) "0"
  'geoplugin_dmaCode' =>
  string(1) "0"
  'geoplugin_countryCode' =>
  string(2) "CA"
  'geoplugin_countryName' =>
  string(6) "Canada"
  'geoplugin_continentCode' =>
  string(2) "NA"
  'geoplugin_latitude' =>
  string(9) "45.507801"
  'geoplugin_longitude' =>
  string(10) "-73.580399"
  'geoplugin_regionCode' =>
  string(2) "QC"
  'geoplugin_regionName' =>
  string(6) "Quebec"
  'geoplugin_currencyCode' =>
  string(3) "CAD"
  'geoplugin_currencySymbol' =>
  string(5) "&#36;"
  'geoplugin_currencySymbol_UTF8' =>
  string(1) "$"
  'geoplugin_currencyConverter' =>
  string(5) "1.332"
}
php > echo $var['geoplugin_countryCode'];
CA
php >
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?

Post by simonmlewis »

So how did you make it do that?
I cannot get that far with it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I identify the country the browser is in?

Post by Celauran »

I have shown every step. I don't know what you're doing differently.
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?

Post by simonmlewis »

Is yours like this?

Code: Select all

<?php
$varname = (unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
$varname['geoplugin_countryCode'];
echo "$varname";

?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I identify the country the browser is in?

Post by Celauran »

Without the extra parentheses. As you see above:

Code: Select all

$var = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=xxx.xxx.xxx.xxx'));
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?

Post by simonmlewis »

If I do this:

Code: Select all

<?php
$varname = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$varname['geoplugin_countryCode'];
echo "$varname, $var";
var_dump($varname);
?>
I get this:
[text]Array, array(18) { ["geoplugin_request"]=> string(13) "xx.xx.xx.xx" ["geoplugin_status"]=> int(206) ["geoplugin_credit"]=> string(145) "Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com. ["geoplugin_city"]=> string(0) "" ["geoplugin_region"]=> string(0) "" ["geoplugin_areaCode"]=> string(1) "0" ["geoplugin_dmaCode"]=> string(1) "0" ["geoplugin_countryCode"]=> string(2) "GB" ["geoplugin_countryName"]=> string(14) "United Kingdom" ["geoplugin_continentCode"]=> string(2) "EU" ["geoplugin_latitude"]=> string(4) "51.5" ["geoplugin_longitude"]=> string(5) "-0.13" ["geoplugin_regionCode"]=> string(0) "" ["geoplugin_regionName"]=> NULL ["geoplugin_currencyCode"]=> string(3) "GBP" ["geoplugin_currencySymbol"]=> string(6) "£" ["geoplugin_currencySymbol_UTF8"]=> string(2) "£" ["geoplugin_currencyConverter"]=> string(6) "0.6566" } [/text]

But this:

Code: Select all

<?php
$varname = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$varname['geoplugin_countryCode'];
echo "$varname";
?>
I just get this:
[text]Array[/text]
Love PHP. Love CSS. Love learning new tricks too.
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?

Post by simonmlewis »

Bingo:

Code: Select all

<?php
$varname = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
extract($varname);
echo "$geoplugin_countryCode";
?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I identify the country the browser is in?

Post by Celauran »

simonmlewis wrote:But this:

Code: Select all

<?php
$varname = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$varname['geoplugin_countryCode'];
echo "$varname";
?>
I just get this:
[text]Array[/text]
You need to

Code: Select all

echo $varname['geoplugin_countryCode'];
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I identify the country the browser is in?

Post by Celauran »

simonmlewis wrote:Bingo:

Code: Select all

<?php
$varname = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
extract($varname);
echo "$geoplugin_countryCode";
?>
This creates a global variable for every key in the array. Not a good idea.
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?

Post by simonmlewis »

So why does it show ES for someone in Spain, and GB for us in the UK?
We don't use this Variable for anything else.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I identify the country the browser is in?

Post by Celauran »

simonmlewis wrote:So why does it show ES for someone in Spain, and GB for us in the UK?
Why wouldn't it? Those are the correct values.
simonmlewis wrote:We don't use this Variable for anything else.
I'm sure it's fine in this specific case. Not a habit you want to get into, is all. If you have a variable $user and you extract an array that has a user key, your $user variable has just been overwritten.
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?

Post by simonmlewis »

I'm with you.
So something like this:

Code: Select all

$varname = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$country = $varname['geoplugin_countryCode'];
echo "$country";
Maybe with a more unique variable name tho.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I identify the country the browser is in?

Post by Celauran »

That looks like a much better approach to me.
Post Reply