1

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?

Community
  • 1
  • 1
Mihai
  • 59
  • 1
  • 5
  • Could you explain what you mean by network address? If you mean the mac address, I think it is not possible to get the clients mac at the server side. – PCoder Aug 28 '12 at 14:12

5 Answers5

3

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())
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
2

$_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

Peter
  • 16,453
  • 8
  • 51
  • 77
2

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.

talha2k
  • 24,937
  • 4
  • 62
  • 81
0

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 https://superuser.com/questions/63124/is-it-possible-to-get-the-nat-ip-address for more info about NAT

Community
  • 1
  • 1
grdaneault
  • 830
  • 1
  • 11
  • 21
0

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

Kermit
  • 33,827
  • 13
  • 85
  • 121