当前位置: 动力学知识库 > 问答 > 编程问答 >

ip - It is possible to get network address with PHP?

问题描述:

Possible Duplicate:

Getting Users Real IP address using PHP

I have a script on a site that saves some info about a client when he leaves the page. Is it possible to get his network address, for example 192.168.1.154.

I saw on a forum a discussion about this and they say to use $_SERVER['HTTP_X_FORWARDED_FOR'] but it is not working! It shows me my real IP.

How can I do this?

网友答案:

$_SERVER['REMOTE_ADDR']

Docs http://php.net/manual/en/reserved.variables.server.php

If client will visit you from local network you gonna get 192.168.*.*, otherwise remote ip like 35.88.33.11

网友答案:

You cannot get the inside local IP of a client if your server is outside the NAT'd network. That would be not only a security risk, but defeat the purpose of NAT.

You can, if you want his local NAT'd IP, run a javascript, something like this may work:

function myIP(){
  var yip2=java.net.InetAddress.getLocalHost(); 
  var yip=yip2.getHostAddress();
  return yip;
}//end myIP

alert("your machine's local network IP is "+ myIP())
网友答案:

Use this:

<?php  

   echo $_SERVER['REMOTE_ADDR'];  //ip of visitor - you need this

   echo $_SERVER['SERVER_ADDR'];  //ip of server

?>

Using JSON you can also get the client ip by using the following code:

$(document).ready(function(){

    //Service 1
    $.getJSON( "http://smart-ip.net/geoip-json?callback=?",
            function(data){
                alert( data.host);
            }
    );

    //Service 2
    $.getJSON( "http://jsonip.appspot.com/?callback=?",
            function (json){
                alert( "User ip: " + json.ip); // alerts the ip address
            }
    );

});

Hope this helps.

网友答案:

This is impossible serverside. Address like 192.168.1.154 are not routable so the server doesn't know anything about them. It may be possible that some routers set the HTTP_X_FORWARDED_FOR header, but you shouldn't count on it. Unless the script is running only on the local network, like Peter Szymkowski suggests.

see http://superuser.com/questions/63124/is-it-possible-to-get-the-nat-ip-address for more info about NAT

网友答案:

I would see which parameter of var_dump($_SERVER); displays your network IP.

分享给朋友:
您可能感兴趣的文章:
随机阅读: