Get Country By IP Address in PHP


SUBMITTED BY: Guest

DATE: Jan. 15, 2014, 11:24 p.m.

FORMAT: PHP

SIZE: 798 Bytes

HITS: 830

  1. <?php
  2. function getLocationInfoByIp(){
  3. $client = @$_SERVER['HTTP_CLIENT_IP'];
  4. $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  5. $remote = @$_SERVER['REMOTE_ADDR'];
  6. $result = array('country'=>'', 'city'=>'');
  7. if(filter_var($client, FILTER_VALIDATE_IP)){
  8. $ip = $client;
  9. }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  10. $ip = $forward;
  11. }else{
  12. $ip = $remote;
  13. }
  14. $ip_data = @json_decode
  15. (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
  16. if($ip_data && $ip_data->geoplugin_countryName != null){
  17. $result['country'] = $ip_data->geoplugin_countryCode;
  18. $result['city'] = $ip_data->geoplugin_city;
  19. }
  20. return $result;
  21. }
  22. ?>

comments powered by Disqus