Skip to content

Instantly share code, notes, and snippets.

@makeitaboldmove
Last active September 11, 2018 03:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save makeitaboldmove/61da218f379885ab8db9897068a5ef24 to your computer and use it in GitHub Desktop.
How To Detect Middle Eastern Users Using PHP
<?php
if(isMENA()){
echo '<img src="/images/mena-image.png" alt="MENA Image" />';
} else {
echo '<img src="/images/other-image.png" alt="Other Image" />';
}
<?php
function isMENA($ip = $_SERVER['REMOTE_ADDR']) {
/* pull the xml */
$url = 'https://api.hostip.info/?ip=' . $ip;
$xml = simplexml_load_file($url);
/* parse the result */
$country = (array) $xml->children('gml', true)->featureMember->children()->Hostip->countryName;
$country = $result['country'][0];
/* return true or false */
return in_array($country, [
'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',
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment