简体   繁体   English

使用IP进行位置跟踪

[英]Location Tracking using IP

I want to implement location tracker using IP of visitors visiting my site. 我想使用访问我网站的访问者的IP来实现位置跟踪器。 I am using the following code: 我正在使用以下代码:

<?php
error_reporting(E_ERROR | E_PARSE);

$info = file_get_contents("http://api.hostip.info/get_html.php");
$arr1 = preg_split("/[\s,]+/", "$info");
end($arr1);

$z         = prev($arr1);
$geoplugin = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=$z') );
$loc       = $geoplugin['geoplugin_regionName'];

echo "<td><input type='text' name='location' value='$loc'  style='width:250px;height:50px;display:inline;background- color:#FFF;color:black;border-radius:0px;border:1px solid #C8C8C8' placeholder='Current Location'></td>"; ?>

However, I am unable to get specific location and it shows different location every time. 但是,我无法获得特定的位置,并且每次都会显示不同的位置。

GeoPlugin IP Detection GeoPlugin IP检测

<?php
    $user_ip = getenv('http://api.hostip.info/get_html.php');
    $geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
    $city = $geo["geoplugin_city"];
    $region = $geo["geoplugin_regionName"];
    $country = $geo["geoplugin_countryName"];

or else you can use 否则你可以使用

ipinfo.io General Logging ipinfo.io常规日志记录

ipinfo.io Developer Logging ipinfo.io开发人员日志记录

Full IP Details 完整的IP详细信息

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "phone": 650
}

IPv6 IPv6

There's full support for IPv6, both in terms of looking up data for an IPv6 address and for making IPv6 requests. 在查找IPv6地址数据和发出IPv6请求方面,都对IPv6完全支持。

$ curl ipinfo.io
{
  "ip": "2601:9:7680:363:75df:f491:6f85:352f",
  "hostname": "No Hostname",
  "city": null,
  "region": null,
  "country": "US",
  "loc": "38.0000,-97.0000",
  "org": "AS7922 Comcast Cable Communications, Inc."
}

http://ipinfo.io/json for your own IP 您自己的IP的http://ipinfo.io/json

http://ipinfo.io/8.8.8.8/json for details on another IP http://ipinfo.io/8.8.8.8/json有关另一个IP的详细信息

JSONP JSONP

JSONP is also supported, which allows you to use ipinfo.io entirely in client-side code. 还支持JSONP,它使您可以完全在客户端代码中使用ipinfo.io。 You just need to specify the callback parameter, eg. 您只需要指定回调参数即可。 http://ipinfo.io/?callback=callback . http://ipinfo.io/?callback=callback Most javascript libraries will automatically handle this for you though. 不过,大多数JavaScript库都会自动为您处理。 Here's a jQuery example that logs the client IP and country: 这是一个记录客户端IP和国家/地区的jQuery示例:

$.get("http://ipinfo.io", function(response) {
    console.log(response.ip, response.country);
}, "jsonp");

This works: 这有效:

<?php
error_reporting(E_ERROR | E_PARSE);
$ip = $_SERVER['REMOTE_ADDR'];

$url = 'http://www.geoplugin.net/php.gp?ip='.$ip;
$geoplugin = unserialize(file_get_contents($url));
// print_r($geoplugin); // To see what keys are available...

$loc = $geoplugin['geoplugin_countryName'];
echo "
    <td>
        <input type='text' name='location' value='$loc' style='width:250px;height:50px;display:inline;background-color:#FFF;color:black;border-radius:0px;border:1px solid #C8C8C8' placeholder='Current Location'/>
    </td>
";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM