Home > Backend Development > PHP Tutorial > Get the PHP code of the country you are in by IP address

Get the PHP code of the country you are in by IP address

WBOY
Release: 2016-07-25 08:42:52
Original
1810 people have browsed it
  1. function getLocationInfoByIp(){
  2. $client = @$_SERVER['HTTP_CLIENT_IP'];
  3. $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  4. $remote = @$_SERVER['REMOTE_ADDR'];
  5. $result = array('country'=>'', 'city'=>'');
  6. if(filter_var($client, FILTER_VALIDATE_IP)){
  7. $ip = $client;
  8. }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  9. $ip = $forward;
  10. }else{
  11. $ip = $remote;
  12. }
  13. $ip_data = @json_decode
  14. (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
  15. if($ip_data && $ip_data->geoplugin_countryName != null){
  16. $result['country'] = $ip_data->geoplugin_countryCode;
  17. $result['city'] = $ip_data->geoplugin_city;
  18. }
  19. return $result;
  20. }
  21. ?>
复制代码

PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template