如何获得客户的IP? php默认函数不像getenv,$ _ SERVER ['REMOTE_ADDR']

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.

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).

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.