如何找到某人的ip位置并在找到时建立链接?

时间:2014-04-30 21:15:28

标签: javascript php jquery html5

我想要一个可以找到某人ip的代码,然后为该位置创建一个链接(链接转到另一个包含该位置内容的页面)

我发现了一些代码,但我不认为它做了我做的事情呢?有什么想法吗?

$.get("http://ipinfo.io", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#address").html("Location: " + response.city + ", " + response.region);
    $("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");

Html代码:

<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>

<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>

1 个答案:

答案 0 :(得分:3)

查看你的评论我看到你的代码是错的,它的jQuery不是PHP:

<!DOCTYPE HTML>
<head>
<title>
Test
</title>
<body>
<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>

<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>

<?php 
$.get("http://ipinfo.io", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#address").html("Location: " + response.city + ", " + response.region);
    $("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
?>

我已经整理了一个 friendly-api-consumer 示例,它会将结果缓存60secs(实际上你应该把它增加到86400 = 1天来保存你宝贵的请求配额),这会加速加载后续请求&amp;不要像http://ipinfo.io/developers上所述那样轰炸API,每天限制为1,000个API请求 ......

<?php 
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['ip'])){

    header('Content-Type: application/json');

    if($_POST['ip'] == $_SERVER['REMOTE_ADDR']){
        //fix ip6 address for testing
        $_POST['ip'] = ($_POST['ip']='::1') ? '127.0.0.1' : $_POST['ip'];

        //cache path
        $cache_folder = dirname(__FILE__) . '/cache';

        //cache file, this will store the API response for the ip
        $cache_file = $cache_folder.'/'.$_POST['ip'].'.json';

        //check folder exists
        if(!file_exists($cache_folder)){mkdir($cache_folder, 0755);}

        //do if cache files not found or older then 60 seconds (60 should suffice)
        if(!file_exists($cache_file) || filemtime($cache_file) < (time() - 60)){
            //query API, apending the users IP address to the url
            $data = file_get_contents('http://ipinfo.io/'.$_POST['ip']);

            //store for future use
            file_put_contents($cache_file, $data);
        }else{
            $data = file_get_contents($cache_file);
        }
        exit($data);
    }else{
        exit(array('access denied, missing $_POST[\'ip\']'));
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script>
    $(function(){
        $.post("index.php", { 'ip': '<?php echo $_SERVER['REMOTE_ADDR'];?>' },
        function(response) {
            $("#ip").html("IP: " + response.ip);
            $("#address").html("Location: " + response.city + ", " + response.region);
            $("#details").html(JSON.stringify(response, null, 4));
        });
    });
    </script>
</head>
<body>
    <h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
    <hr/>
    <div id="ip"></div>
    <div id="address"></div>
    <hr/>Full response: <pre id="details"></pre>
</body>
</html>

您甚至可以将其从脏文件缓存更改为持久缓存,并将结果存储在数据库中。

希望它有所帮助,快乐编码!