Page 3 of 3
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 3:43 pm
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";
?>
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 4:05 pm
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) "$"
'geoplugin_currencySymbol_UTF8' =>
string(1) "$"
'geoplugin_currencyConverter' =>
string(5) "1.332"
}
php > echo $var['geoplugin_countryCode'];
CA
php >
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 4:18 pm
by simonmlewis
So how did you make it do that?
I cannot get that far with it.
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 4:39 pm
by Celauran
I have shown every step. I don't know what you're doing differently.
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 4:52 pm
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";
?>
Re: How do I identify the country the browser is in?
Posted: Wed Nov 18, 2015 5:28 pm
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'));
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 3:03 am
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]
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 4:52 am
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";
?>
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 5:53 am
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'];
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 5:53 am
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.
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 5:56 am
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.
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 6:03 am
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.
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 6:11 am
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.
Re: How do I identify the country the browser is in?
Posted: Thu Nov 19, 2015 6:13 am
by Celauran
That looks like a much better approach to me.