Skip to content

Instantly share code, notes, and snippets.

@jstoone
Last active August 29, 2015 14:21
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 jstoone/54cd9017d727a7f4275a to your computer and use it in GitHub Desktop.
Save jstoone/54cd9017d727a7f4275a to your computer and use it in GitHub Desktop.
Maxmind: Quick Geo Location
<?php
function getMaxmindCountry($ipaddress) {
$userid = '[user-id]';
$license_key = '[license-key]';
$query = 'https://geoip.maxmind.com/geoip/v2.0/country/' . $ipaddress;
if (empty($ipaddress)) {
return 'ERR';
}
$opts = array(
'http' => array(
'method'=> "GET",
'header'=> "Authorization: Basic " . base64_encode($userid.':'.$license_key) . "\r\n"
)
);
$context = stream_context_create($opts);
$file = file_get_contents($query, false, $context);
if ($file === FALSE) {
return 'ERR';
}
$geo = json_decode($file);
$country = $geo->{"country"}->{"iso_code"};
return $country;
}
// Example Usage
if(getMaxmindCountry($_SERVER["REMOTE_ADDR"]) == "US") {
header('Location: http://maleedgeus.com');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment