Is it safe to trust $_SERVER[‘REMOTE_ADDR’]?

Yes, it’s safe. It is the source IP of the TCP connection and can’t be substituted by changing an HTTP header.

One case you may want to be worry of is if you are behind a reverse proxy in which case the REMOTE_ADDR will always be the IP of the proxy server and the user IP will be provided in an HTTP header (such as X-Forwarded-For). But for the normal use case reading REMOTE_ADDR is fine.

Leave a Comment