【问题标题】:Automatically locate a user's geographical location in PHPPHP自动定位用户地理位置
【发布时间】:2016-10-14 16:00:55
【问题描述】:

我想在注册到我的网站后在 PHP 中自动定位用户的地理位置和 IP 地址。我该怎么办?

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

    ip-api.com 提供免费的基于 xml 的 api。

    要使用他们的 api 获取基于 ip 的位置,您可以使用:

    $xml=simplexml_load_file("http://ip-api.com/xml/ipaddress");
    echo $xml->RegionName;
    

    【讨论】:

      【解决方案2】:

      使用 codeigniter,您只需执行 $ip = $this->input->ip_address() 即可显示您的 IP 地址

      【讨论】:

        【解决方案3】:

        您可以使用第 3 方解决方案,例如 http://ipinfodb.com/ip_location_api.php

        您也可以使用以下代码:

        <?php
        $user_ip = getenv('REMOTE_ADDR');
        $geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
        $country = $geo["geoplugin_countryName"];
        $city = $geo["geoplugin_city"];
        ?>
        

        【讨论】:

          【解决方案4】:
          Just Try This code,this might help you. This a stand alon code you can create a simple file and copy this. But you have to create an google api key for this.  
           <?php
              if(isset($_POST['long'*emphasized text*]) && isset($_POST['lat'])){
                  $key = 'YOUR-KEY_FROM-GOOGLE-API**strong text**';
                  extract($_POST);
                  $json = file_get_contents('https://maps.googleapis.com/maps/api/**strong text**geocode/json?latlng='.$lat.','.$long.'&key='.$key);
                  $data = json_decode($json,true);
                  foreach($data['results'][1]['address_components'] as $numbers){
                      if( $numbers['types'][0] == 'administrative_area_level_2'){
                          echo $numbers['long_name'];
                      }
                  }
              }else{
              ?>
              Location: <span id="location"></span>
              <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
              <script>
              if (navigator.geolocation) {
                window.onload = function() {
                var startPos;
                var geoOptions = {
                  maximumAge: 5 * 60 * 1000,
                  timeout: 10 * 1000
                }
          
                var geoSuccess = function(position) {
                  startPos = position;
                  var data = {
                      long: startPos.coords.longitude,
                      lat: startPos.coords.latitude
                  };
                  var results = jQuery.ajax({
                      type: 'POST',
                      url: '#',
                      data: data,
                      async:false
                      }).responseText;
                  document.getElementById('location').innerHTML = results;
                };
                var geoError = function(position) {
                  console.log('Error occurred. Error code: ' + error.code);
                  // error.code can be:
                  //   0: unknown error
                  //   1: permission denied
                  //   2: position unavailable (error response from location provider)
                  //   3: timed out
                };
          
                navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
              };
          
              }
              else {
                console.log('Geolocation is not supported for this Browser/OS version yet.');
              }
              </script>
              <?php
              }
              ?>
          

          【讨论】:

            【解决方案5】:

            你可以通过这个获取IP值:

            $theClientIp = $_SERVER['REMOTE_HOST'];
            

            然后你可以像这样进行会话:

            session_start();
            $_SESSION['ip']=$theClientIp;
            

            在任何地方都可以通过回显这个来获取 ip 值

            session_start(); $_SESSION[ip'];
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-04-11
              • 1970-01-01
              • 2016-05-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多