Skip to content

Instantly share code, notes, and snippets.

@anunay
Created July 24, 2014 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anunay/db951ba66e4d6097484a to your computer and use it in GitHub Desktop.
Save anunay/db951ba66e4d6097484a to your computer and use it in GitHub Desktop.
MaxMind GeoIP
<?php
/** maxmind geoip integration in php */
function get_real_ip() {
$ipaddress = '';
if(empty($_REQUEST['ip']) && @$_REQUEST['ip']==""){
if (@$_SERVER['HTTP_CLIENT_IP'])
$ipaddress = @$_SERVER['HTTP_CLIENT_IP'];
else if(@$_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = @$_SERVER['HTTP_X_FORWARDED_FOR'];
else if(@$_SERVER['HTTP_X_FORWARDED'])
$ipaddress = @$_SERVER['HTTP_X_FORWARDED'];
else if(@$_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = @$_SERVER['HTTP_FORWARDED_FOR'];
else if(@$_SERVER['HTTP_FORWARDED'])
$ipaddress = @$_SERVER['HTTP_FORWARDED'];
else if(@$_SERVER['REMOTE_ADDR'])
$ipaddress = @$_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
}else{
$ipaddress = @$_REQUEST['ip'];
}
return $ipaddress;
}
include("maxmind-geoip/src/geoipcity.inc");
$gi = geoip_open("maxmind-geoip/GeoLiteCity.dat", GEOIP_STANDARD);
$record = GeoIP_record_by_addr($gi,get_real_ip());
$geoipdetails = array();
$country_code = $record->country_code3;
$country_region = $record->region;
$country_city = $record->city;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment