4

I'm looking for a clear cut way to detect if a visitor is from the Middle East. If they are, I need to display a different header image (the current one has a hog in it, so it is frowned upon).

I can code the if then else for that, but I'm just looking for a simple function to detect. Here's a function I used to get the country by IP:

function get_country_by_ip($ip){

    if(!$ip) return false;    # Missing parameter

    # Pull the XML
    $url = 'http://api.hostip.info/?ip='.$ip;
    $xml = simplexml_load_file($url);

    # Parse the data and store into array
    $citystate = explode(", ", $xml->children('gml', true)->featureMember->children()->Hostip->children('gml', true)->name);
    $result['city'] = $citystate[0];
    $result['state'] = $citystate[1];
    $result['country'] = $xml->children('gml', true)->featureMember->children()->Hostip->countryName;
    $result['country_abbr'] = $xml->children('gml', true)->featureMember->children()->Hostip->countryAbbrev;

    return (object) $result;

}

Can anyone help? Thanks.

hatfieldajoshua
  • 307
  • 1
  • 4
  • 15
  • if you have the country from the above whats the issue? -also this will never guarantee your right about the country –  Oct 05 '11 at 20:44
  • 7
    There are 3 times as many Muslims in Asia-Pacific than in Middle East/N. Africa. See [wikipedia](http://en.wikipedia.org/wiki/List_of_countries_by_Muslim_population) – Larry K Oct 05 '11 at 20:54

3 Answers3

2

You could define which countries fall into the "Middle East" group - by defining configurable array directly in the PHP file or in database and then check if the given country returned by your function is amongst the list of countries that belong to "Middle East". By your example, something like this:

$middleEast = array(
 'Syria',
 'Iraq',
 ... // all the rest
);

$country = get_country_by_ip($ip);

if (in_array($country['country'], $middleEast) {
 echo 'Middle East!';
 exit;
}
ddinchev
  • 33,683
  • 28
  • 88
  • 133
  • 5
    since when was Japan and China considered part of the middle east ?? –  Oct 05 '11 at 20:48
  • Confused it with far east, nevertheless is this relative to the approach suggested? – ddinchev Oct 05 '11 at 21:14
  • I didn't want to do it this way, hoping that I could determine region dynamically, but this worked out perfectly. Posting the code in a separate reply. – hatfieldajoshua Oct 10 '11 at 13:44
1
<?php

/* get country by ip */

function get_country_by_ip($ip){

    if(!$ip) return false;

    /* pull the xml */

    $url = 'http://api.hostip.info/?ip='.$ip;
    $xml = simplexml_load_file($url);


    /* parse the data and store into array */

    $citystate = explode(", ", $xml->children('gml', true)->featureMember->children()->Hostip->children('gml', true)->name);

    $result['city'] = $citystate[0];
    $result['state'] = $citystate[1];

    $result['country'] = (array) $xml->children('gml', true)->featureMember->children()->Hostip->countryName;
    $result['country'] = $result['country'][0];

    $result['country_abbr'] = (array) $xml->children('gml', true)->featureMember->children()->Hostip->countryAbbrev;
    $result['country_abbr'] = $result['country_abbr'][0];

    return (object) $result;

}


/* get country */

$geo_info = get_country_by_ip($_SERVER['REMOTE_ADDR']);


/* MENA countries */

$mena = array(

    'ALGERIA',

    'BAHRAIN',

    'EGYPT',

    'IRAN',

    'IRAQ',

    'ISRAEL',

    'JORDAN',

    'KUWAIT',

    'LEBANON',

    'LIBYA',

    'MOROCCO',

    'OMAN',

    'PALESTINE',

    'QATAR',

    'SAUDI ARABIA',

    'SYRIA',

    'TUNISIA',

    'UNITED ARAB EMIRATES',

    'YEMEN',

    'ARMENIA',

    'AZERBAIJAN',

    'CYPRUS',

    'DJIBOUTI',

    'MALTA',

    'MAURITANIA',

    'SAHRAWI ARAB DEMOCRATIC REPUBLIC',

    'SOMALIA',

    'SUDAN',

    'TURKEY',

);


/* image with a hog */

$img = 'mmm-pork.jpg';

if(in_array($geo_info->country, $mena)){


    /* image with no hog */

    $img = 'cant-have-pork.jpg';

} ?>
hatfieldajoshua
  • 307
  • 1
  • 4
  • 15
0

i suggest you to try http://phpweby.com/software/ip2country for free.

ShirazITCo
  • 1,041
  • 6
  • 23
  • 38
  • I have never used that service, but I have used GeoIP (GeoLite Country) database - which is free - and, if you keep it updated, then you're not likely to have many problems. – crmpicco Aug 28 '12 at 15:54