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

Javascript 如何找到某人的ip位置并在找到时创建链接?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我想要一个代码,将找到某人的ip,然后使一个链接到该位置(链接到一个不同的页面与该位置的内容) 我找到了一些代码,但我不认为它做了我认为它做了什么?有什么想法吗 $.get("http://ipinfo.io", function (response) { $("#ip").html("IP: " + response.ip); $("#address").html("Location: " + response.city + ", " + response.region);

我想要一个代码,将找到某人的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>
客户端IP地理定位使用


完整响应:
查看您的评论,我发现您的代码是错误的,它的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");
?>
您甚至可以将其从脏文件缓存更改为持久缓存,并将结果存储在数据库中


希望有帮助,快乐编码

同源策略,您需要使用PHP将json代理到您的jquery中。@LozCherone这是一个jsonp请求,所以sop不适用于您不期望它做什么,或者它不期望做什么?你看到错误了吗?你的代码运行得很好-Drahcir没有,我把它放在我的网站上,然后看:看,现在我也没做任何事情,但我认为你的程序是正确的。将文件扩展名改为.php而不是.html,这样就可以了;是啊,虽然不起作用。。天哪,我讨厌免费托管!!通过FTP检查缓存文件夹是否存在,如果没有创建缓存文件夹,如果您有日志,请检查日志,您可能没有安装curl。如果您没有更新,我将使用文件_get_contents()进行更新。@user3590931我更新了答案,如果没有安装curl,则使用fgc。希望如此;pok ive发现您的服务器存在curl但open_basedir的问题,您应该看到
警告:curl_setopt():当日志中有open_basedir时,CURLOPT_FOLLOWLOCATION无法激活。我更新了答案并删除了该选项。:)
<?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>