Skip to content

Instantly share code, notes, and snippets.

@mksddn
Created February 6, 2020 17:01
Show Gist options
  • Select an option

  • Save mksddn/1f5c95191f05aea1881f91c6467bf5b6 to your computer and use it in GitHub Desktop.

Select an option

Save mksddn/1f5c95191f05aea1881f91c6467bf5b6 to your computer and use it in GitHub Desktop.
<?php
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = @$_SERVER['REMOTE_ADDR'];
$region = array('country'=>'', 'city'=>'');
if(filter_var($client, FILTER_VALIDATE_IP)) $ip = $client;
elseif(filter_var($forward, FILTER_VALIDATE_IP)) $ip = $forward;
else $ip = $remote;
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null)
{
$region = $ip_data->geoplugin_regionName;
}
// echo $region;
?>
<script>
jQuery(function(){
jQuery('input.field-region').val(<?= json_encode($region) ?>);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment