Don't play with php code - do it on httpd server level, so you will get correct IP displayed everywhere (logs, applications etc)
Nginx
(where 192.168.1.200
is your proxy server):
server {
#other junk here blah blah
set_real_ip_from 192.168.1.200/8;
real_ip_header X-Real-IP;
real_ip_recursive on;
location / {
#some junk here
}
}
More: https://calvin.me/forward-ip-addresses-when-using-nginx-proxy
Apache
(where 192.168.1.200
is your proxy server):
RemoteIPHeader X-Forwarded-For
RemoteIPTrustedProxy 192.168.1.200
More: https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html#remoteiptrustedproxy
On Proxy side
If you are using nginx as a proxy server - it's nice to have this few variable set if you going trough proxy:
proxy_hide_header X-Powered-By;
add_header Content-Security-Policy "upgrade-insecure-requests";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Cache-Control "no-transform" always;
add_header Referrer-Policy no-referrer always;
add_header X-Robots-Tag none;
By the way - if you like Docker - strongly recommend this project: NginxProxyManager