How to track users location / region in PHP

前端 未结 6 1672
野趣味
野趣味 2020-12-10 19:44

I\'m trying to get the country from which the user is browsing the website so I can work out what currency to show on the website. I have tried using the GET scripts availab

6条回答
  •  醉酒成梦
    2020-12-10 20:18

    I use this:

      $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
      $ip = $_SESSION['ip'];
      $try1 = "http://ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
      $try2 = "http://backup.ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
      $XML = @simplexml_load_file($try1,NULL,TRUE);
      if(!$XML) { $XML = @simplexml_load_file($try2,NULL,TRUE); }
      if(!$XML) { return false; }
    
      //Retrieve location, set time
      if($XML->City=="") { $loc = "Localhost / Unknown"; }
      else { $loc = $XML->City.", ".$XML->RegionName.", ".$XML->CountryName; }
      $_SESSION['loc'] = $loc;
    

提交回复
热议问题