0

I'm so new to headers. So, this is what I came out with to read the client's IP for WordPress custom REST API.

if ( !empty( $request->get_header('X-Forwarded-For') ) ) {
    $merchant_ip = $request->get_header('X-Forwarded-For');
} elseif (!empty( $request->get_header('Remote_Addr') )) {
    $merchant_ip = $request->get_header('Remote_Addr');
} else {
    $merchant_ip = $request->get_header('X-Client-IP');
}

Did I miss out any other headers? Or do you think there's a better way to read the IP.

mewiben39
  • 121
  • 9
  • Does this answer your question? [How to get the client IP address in PHP](https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php) – CBroe May 11 '22 at 11:01
  • @CBroe Sorry, I'm actually using WordPress and I'm customizing its REST API. – mewiben39 May 11 '22 at 11:15
  • That changes little about what headers you are actually accessing. The point was, if there were any more relevant headers to look at, you would likely find them mentioned in that duplicate. – CBroe May 11 '22 at 11:18
  • @CBroe I'm actually new to headers. So, I wonder if there's a difference between `X-Forwarded-For` and `HTTP_X_FORWARDED_FOR`? Cos from what I read, for WordPress, it's `X-Forwarded-For` so could I use `HTTP_X_FORWARDED_FOR` instead? – mewiben39 May 11 '22 at 11:22
  • That part really depends on how your script / framework / library accesses the headers. The `$_SERVER` variable usually has these prefixed with `HTTP_` (simply to differentiate them other values in there that are not from headers) and `-` replaced with `_`, whereas the `$request->get_header` method shown above appears to access them directly by their actual header name. – CBroe May 11 '22 at 12:57

0 Answers0