dsgnze6572 2014-04-30 07:38
浏览 42
已采纳

需要有关在PHP中获取NAT后面的IP地址的建议

What I'm trying to achieve here is to get local ip address of computer that is connecting to machine with public IP address.

We have server with public IP address (let's say X). Also we have 15 computers in our office with local ip addresses (192.168.20....). I want to know which computer changed something on our system. We are all going out on the internet with public IP Y. As we all know with $_SERVER['REMOTE_ADDR'] I can only get address Y, but can't get local IP address (192.168.20.N).

So I've installed webserver on one of the local machines and then I'm sending ajax (jsonp) request to that local machine, I get local IP address of computer and than send that IP address to public server in every request. everything works fine and I'm getting both local and public IP address. But I wonder if there is any better way of doing this?

here is my test.js script

$(document).ready(function(){
    var _ip = '';
    $.ajax({
        url: 'http://192.168.20.24/getIP.php', //local machine
        dataType: 'jsonp',
        success: function(response){
            _ip = response;
            $.ajax({
                url: 'getIP.php', //public address
                type: 'POST',
                dataType: 'json',
                data: {
                    ip: _ip
                },
                dataType: 'json',
                success: function(response){
                    $('#local').html(response.local);
                    $('#public').html(response.public);
                }
            })
        }
    });

});

local getIP.php

header('content-type: application/json; charset=utf-8');
echo $_GET['callback'] . '('.json_encode($_SERVER['REMOTE_ADDR']).')';

and public getIP.php

echo json_encode(array('local' => $_POST['ip'], 'public' => $_SERVER['REMOTE_ADDR']));

Thanks in advance

Note: code is written for testing purposes only.

  • 写回答

2条回答 默认 最新

  • dongzhentiao2326 2014-05-04 08:57
    关注

    Javascript operates at HTTP level, so there is no such thing as an IP address in Javascript world.

    The workaround you devised is widely used and it is, I believe, the only way to get the client's private address. As hek2mgl advises, this solution is insecure indeed, although in your case, the concern is less relevant because you seem to have complete control of the clients, the server, and the network in between.

    For a really secure solution, use client certificates. I understand you do not really care about the client's IP addresses per se, but only want to use them as a mean of authentication (or was it just identification? :D).


    Configuration is not trivial but not rocket sience either. Here is a nice tutorial (Apache). It assumes the use of certificates issued by a public certification authority, but you can generate and use your own self-signed certificates (tutorial 1, tutorial 2)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常