Instantly share code, notes, and snippets.

Embed
What would you like to do?
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