Skip to content

Instantly share code, notes, and snippets.

@elhardoum
Last active May 5, 2017 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elhardoum/ae47d756b7a744f11d30f5a1a2b52d86 to your computer and use it in GitHub Desktop.
Save elhardoum/ae47d756b7a744f11d30f5a1a2b52d86 to your computer and use it in GitHub Desktop.
Check if current user is from certain continent
<?php
/**
* Get client IP
*/
function whatismyip() {
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = null;
return $ipaddress;
}
/**
* Country codes by continent list
* @link https://gist.github.com/orenitamar/3207618
*/
function country_list() {
return json_decode('{ "europe": { "va": "vatican city", "ch": "switzerland", "ad": "andorra", "ee": "estonia", "is": "iceland", "am": "armenia", "al": "albania", "cz": "czech republic", "ge": "georgia", "at": "austria", "ie": "ireland", "gi": "gibraltar", "gr": "greece", "nl": "netherlands", "pt": "portugal", "no": "norway", "lv": "latvia", "lt": "lithuania", "lu": "luxembourg", "es": "spain", "it": "italy", "ro": "romania", "pl": "poland", "be": "belgium", "fr": "france", "bg": "bulgaria", "dk": "denmark", "hr": "croatia", "de": "germany", "hu": "hungary", "ba": "bosnia/herzegovina", "fi": "finland", "by": "belarus", "fo": "faeroe islands", "mc": "monaco", "cy": "cyprus", "mk": "macedonia", "sk": "slovakia", "mt": "malta", "si": "slovenia", "sm": "san marino", "se": "sweden", "gb": "united kingdom" }, "oceania": { "ck": "cook islands", "pw": "palau", "tv": "tuvalu", "na": "nauru", "ki": "kiribati", "mh": "marshall islands", "nu": "niue", "to": "tonga", "nz": "new zealand", "au": "australia", "vu": "vanuatu", "sb": "solomon islands", "ws": "samoa", "fj": "fiji", "fm": "micronesia" }, "africa": { "gw": "guinea-bissau", "zm": "zambia", "ci": "ivory coast", "eh": "western sahara", "gq": "equatorial guinea", "eg": "egypt", "cg": "congo", "cf": "central african republic", "ao": "angola", "ga": "gabon", "et": "ethiopia", "gn": "guinea", "gm": "gambia", "zw": "zimbabwe", "cv": "cape verde", "gh": "ghana", "rw": "rwanda", "tz": "tanzania", "cm": "cameroon", "na": "namibia", "ne": "niger", "ng": "nigeria", "tn": "tunisia", "lr": "liberia", "ls": "lesotho", "tg": "togo", "td": "chad", "er": "eritrea", "ly": "libya", "bf": "burkina faso", "dj": "djibouti", "sl": "sierra leone", "bi": "burundi", "bj": "benin", "za": "south africa", "bw": "botswana", "dz": "algeria", "sz": "swaziland", "mg": "madagascar", "ma": "morocco", "ke": "kenya", "ml": "mali", "km": "comoros", "st": "sao tome and principe", "mu": "mauritius", "mw": "malawi", "so": "somalia", "sn": "senegal", "mr": "mauritania", "sc": "seychelles", "ug": "uganda", "sd": "sudan", "mz": "mozambique" }, "asia": { "mn": "mongolia", "cn": "china", "af": "afghanistan", "am": "armenia", "vn": "vietnam", "ge": "georgia", "in": "india", "az": "azerbaijan", "id": "indonesia", "ru": "russia", "la": "laos", "tw": "taiwan", "tr": "turkey", "lk": "sri lanka", "tm": "turkmenistan", "tj": "tajikistan", "pg": "papua new guinea", "th": "thailand", "np": "nepal", "pk": "pakistan", "ph": "philippines", "bd": "bangladesh", "ua": "ukraine", "bn": "brunei", "jp": "japan", "bt": "bhutan", "hk": "hong kong", "kg": "kyrgyzstan", "uz": "uzbekistan", "mm": "burma (myanmar)", "sg": "singapore", "mo": "macau", "kh": "cambodia", "kr": "korea", "mv": "maldives", "kz": "kazakhstan", "my": "malaysia" }, "north america": { "gt": "guatemala", "ag": "antigua and barbuda", "vg": "british virgin islands (uk)", "ai": "anguilla (uk)", "vi": "virgin island", "ca": "canada", "gd": "grenada", "aw": "aruba (netherlands)", "cr": "costa rica", "cu": "cuba", "pr": "puerto rico (us)", "ni": "nicaragua", "tt": "trinidad and tobago", "gp": "guadeloupe (france)", "pa": "panama", "do": "dominican republic", "dm": "dominica", "bb": "barbados", "ht": "haiti", "jm": "jamaica", "hn": "honduras", "bs": "bahamas, the", "bz": "belize", "sx": "saint kitts and nevis", "sv": "el salvador", "us": "united states", "mq": "martinique (france)", "ms": "monsterrat (uk)", "ky": "cayman islands (uk)", "mx": "mexico" }, "south america": { "gd": "south georgia", "py": "paraguay", "co": "colombia", "ve": "venezuela", "cl": "chile", "sr": "suriname", "bo": "bolivia", "ec": "ecuador", "gf": "french guiana", "ar": "argentina", "gy": "guyana", "br": "brazil", "pe": "peru", "uy": "uruguay", "fk": "falkland islands" }, "middle east": { "om": "oman", "lb": "lebanon", "iq": "iraq", "ye": "yemen", "ir": "iran", "bh": "bahrain", "sy": "syria", "qa": "qatar", "jo": "jordan", "kw": "kuwait", "il": "israel", "ae": "united arab emirates", "sa": "saudi arabia" } }');
}
/**
* @param $continent str continent name
* @see country_list() for available names
* @link https://gist.github.com/elhardoum/ae47d756b7a744f11d30f5a1a2b52d86
*/
function amIFromContinent($continent='europe') {
$countries = country_list();
$continent = strtolower($continent);
$countries = isset($countries->$continent) ? $countries->$continent : null;
if ( empty($countries) ) {
throw new \Exception('Please pass in the continent name as first parameter to ' . __FUNCTION__);
}
$ip = whatismyip();
if ( !$ip || '127.0.0.1' == $ip )
return;
global $_ip_info;
if ( !$_ip_info || !is_object($_ip_info) )
$_ip_info = @json_decode(shell_exec('curl http://ipinfo.io/' . $ip));
if ( empty($_ip_info) || empty($_ip_info->country) )
return;
$user_country = strtolower($_ip_info->country);
return isset($countries->$user_country);
}
// check if from EUROPE
echo var_dump(amIFromContinent('europe'));
// check if from AFRICA
echo var_dump(amIFromContinent('africa'));
// check if from North America
echo var_dump(amIFromContinent('north america'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment