Created
May 27, 2021 12:26
-
-
Save 0test/fd0d4c1b135f97eee49fe3934a65ed3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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