How can we get the IP address of the client?
TechnicalSenior php developerPHP DeveloperBackend DeveloperWe can get the client's IP address using the $_SERVER superglobal in PHP. Specifically, we check the REMOTE_ADDR key, which provides the direct IP address of the client. However, if the client is behind a proxy, we should also check X-Forwarded-For to get the original IP.
Here’s a simple example:
- $ip = $_SERVER['REMOTE_ADDR'];
- if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }