Skip to content

Instantly share code, notes, and snippets.

@0test
Created May 27, 2021 12:26
Show Gist options
  • Select an option

  • Save 0test/fd0d4c1b135f97eee49fe3934a65ed3f to your computer and use it in GitHub Desktop.

Select an option

Save 0test/fd0d4c1b135f97eee49fe3934a65ed3f to your computer and use it in GitHub Desktop.
<?php
function getUserGeo() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = explode(",",$_SERVER['HTTP_X_FORWARDED_FOR'])[0];
} elseif (!empty($_SERVER['REMOTE_ADDR'])){
$ip = $_SERVER['REMOTE_ADDR'];
}
else{
$ip = 0;
}
$url = "http://ip-api.com/json/".$ip;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if($output === false){
$output = 0;
};
$output = json_decode($output);
if( $output->status == 'fail' || $output->city == ''){
$output = 0;
}
else{
$output = $output->city;
}
return $output;
}
$ip = getUserGeo();
if ($ip){
var_dump($ip);
}
else{
var_dump('не вышло с айпи');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment