Skip to content

Instantly share code, notes, and snippets.

@govindak
Forked from anunay/php-maxmind-geopip.php
Created July 24, 2014 05:21
Show Gist options
  • Save govindak/fc96d5472d59aabb5ba8 to your computer and use it in GitHub Desktop.
Save govindak/fc96d5472d59aabb5ba8 to your computer and use it in GitHub Desktop.
<?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