简体   繁体   中英

Location Tracking using IP

I want to implement location tracker using IP of visitors visiting my site. 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

<?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 Developer Logging

Full IP Details

$ 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

There's full support for IPv6, both in terms of looking up data for an IPv6 address and for making IPv6 requests.

$ 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

http://ipinfo.io/8.8.8.8/json for details on another IP

JSONP

JSONP is also supported, which allows you to use ipinfo.io entirely in client-side code. You just need to specify the callback parameter, eg. http://ipinfo.io/?callback=callback . Most javascript libraries will automatically handle this for you though. Here's a jQuery example that logs the client IP and country:

$.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>
";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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