0

I am getting my local IP with this code

if (getenv('HTTP_X_FORWARDED_FOR')) 

    $ip=getenv('HTTP_X_FORWARDED_FOR');
else

    $ip=getenv('REMOTE_ADDR');

I am getting clients default gateway IP but I want client's live IP.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Nimesh07
  • 362
  • 3
  • 5
  • 3
    it is not possible to obtain the IP address behind a gateway if the gateway is using NAT. That's networking basics. – hek2mgl Jul 15 '13 at 11:21
  • possible duplicate of http://stackoverflow.com/questions/5349604/how-to-track-the-real-ip-address-behind-the-proxy – bansi Jul 15 '13 at 11:25
  • There is only one tool: [JS-Recon](http://www.andlabs.org/tools/jsrecon.html) that based on HTML5 –  Jul 15 '13 at 11:25
  • possible duplicate of stackoverflow.com/questions/5349604/… – bansi -- I have already tried that but it is not working for me – Nimesh07 Jul 16 '13 at 07:09

2 Answers2

1

PHPs getenv(REMOTE_ADDR) will give you the visible network address of the client:

 REMOTE_ADDR  = hostnumber
 hostnumber   = ipv4-address | ipv6-address
 ipv4-address = 1*3digit "." 1*3digit "." 1*3digit "." 1*3digit
 ipv6-address = hexpart [ ":" ipv4-address ]
 hexpart      = hexseq | ( [ hexseq ] "::" [ hexseq ] )
 hexseq       = 1*4hex *( ":" 1*4hex )

However, as indicated, your server can only obtain IP addresses it actually can see. If the client is hiding behind a gateway (p.e. with a NAT), then you'll only see the NAT address. PHPs getenv() can't change those fundamental network basics.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • I have already used getenv(REMOTE_ADDR) but cannot get currant IP of client. – Nimesh07 Jul 30 '13 at 06:30
  • I can only repeat myself. your server can only obtain IP addresses it actually can see. If the client is hiding behind a gateway (p.e. with a NAT), then you'll only see the NAT address. – Bjoern Jul 30 '13 at 12:56
1

First comment is right but with some proxy client's IP is available in HTTP_X_FORWARDED_FOR, it seems not to be your case.

Watch other environment variables (phpinfo() or print_r($_SERVER)) to see if your client IP is available.

If not, this is not possible. The proxy is hidding the real IP (as it should be).

Kevin Labécot
  • 2,005
  • 13
  • 25