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 »

This is in my composer.json file locally:

Code: Select all

{
    "name": "phpmyadmin/phpmyadmin",
    "type": "application",
    "description": "MySQL web administration tool",
    "keywords": ["phpmyadmin","mysql","web"],
    "homepage": "http://www.phpmyadmin.net/",
    "license": "GPL-2.0+",
    "authors": [
        {
            "name": "The phpMyAdmin Team",
            "email": "phpmyadmin-devel@lists.sourceforge.net",
            "homepage": "http://www.phpmyadmin.net/home_page/team.php"
        }
    ],
    "support": {
        "forum": "https://sourceforge.net/p/phpmyadmin/discussion/Help",
        "issues": "https://sourceforge.net/p/phpmyadmin/bugs/",
        "wiki": "http://wiki.phpmyadmin.net/",
        "source": "https://github.com/phpmyadmin/phpmyadmin"
    },
    "require": {
        "php": ">=5.3.0"
    },
    "require-dev": {
        "satooshi/php-coveralls": ">=0.6",
        "phpunit/phpunit": "<4.2",
        "phpunit/phpunit-selenium": ">=1.2"
    }
    
    "require": {
        "willdurand/geocoder": "@stable"
    }
}
How do I load it prior to loading testcountry.inc?
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 doesn't look right. You'd be tying this project to your PMA install. What does your directory structure look like? Also, you have two require blocks. Those should be combined. First, though, let's make sure we're working in the right spot.
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 »

What do you mean Directory Structure?
As in the structure of where files are stored or?
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 »

Right, exactly. The composer.json you posted above is for phpMyAdmin itself.
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 ran the .exe installer file. Is that wrong??
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 »

No, I'm sure that's fine. It's just a separate project. We want to put the composer.json in the root of the project you're currently working on, not in phpMyAdmin.
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 can i copy it in there... or do i need to do it in some official way?
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 »

You definitely don't want it in the file above. It should be in your project's topmost folder. Typically, this is either your document root or one level above that.
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 »

As an example, all of my sites are in ~/Sites. I typically break this down to ~/Sites/Client Name/Project Name. Each of these latter would have its own composer.json.

~/Sites/Client A/Project 1/composer.json
~/Sites/Client A/Project 2/composer.json
~/Sites/Client B/Project 1/composer.json

would all exist.
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 »

Code: Select all

echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
We have found this, and it produces a nice little array.

I'm still not entirely familiar with arrays, so how do I extract the one row for:

[text]array (
'geoplugin_request' => '34.34.456.45',
'geoplugin_status' => 206,
'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.',
'geoplugin_city' => '',
'geoplugin_region' => '',
'geoplugin_areaCode' => '0',
'geoplugin_dmaCode' => '0',
'geoplugin_countryCode' => 'GB',
'geoplugin_countryName' => 'United Kingdom',
'geoplugin_continentCode' => 'EU',
'geoplugin_latitude' => '51.5',
'geoplugin_longitude' => '-0.13',
'geoplugin_regionCode' => '',
'geoplugin_regionName' => NULL,
'geoplugin_currencyCode' => 'GBP',
'geoplugin_currencySymbol' => '£',
'geoplugin_currencySymbol_UTF8' => '£',
'geoplugin_currencyConverter' => '0.6573',
)[/text]
.... 'geoplugin_countryCode' => 'GB',

So then I can ask if it is US, or IE... etc...?
How do I extract that line?
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

$varname['geoplugin_countryCode']
Where $varname is whatever variable you stored the array in.
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 »

Code: Select all

echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
$varname = array();
$varname['geoplugin_countryCode'];
echo "$varname";
I think I am doing something wrong here.....

Tried this too:

Code: Select all

$varname = var_export(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 »

You don't want the var_export in there.
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, with the >> << in there to highlight it for now, it just says "array".

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 »

That's odd.

Code: Select all

var_dump($varname);
Post Reply