Here is the one-liner version getting the client's IP address:
$ip = @$_SERVER['HTTP_CLIENT_IP'] ?: @$_SERVER['HTTP_X_FORWARDED_FOR'] ?: @$_SERVER['REMOTE_ADDR'];
Notes:
- By using
@
, it suppresses the PHP notices.
Value from HTTP_X_FORWARDED_FOR
may consist multiple addresses separated by comma, so if you prefer to get the first one, you can use the following method:
current(explode(',', @$_SERVER['HTTP_X_FORWARDED_FOR']))