How To Get The Client IP Address In PHP
How to get the client IP address in PHP
Sometimes Webmaster need to know the visitor’s IP address for security purpose. We can easily get the visitor's IP by using $_SERVER variable that is created by the server.
Sometimes $_SERVER variable does not returns the correct IP of the visitor. This can be occur due to reasons like the user is behind a proxy etc. That's why we are using the other server variables to get the IP address.
PHP client IP Example
echo $_SERVER['REMOTE_ADDR']; echo "or"; $ip = getenv('HTTP_CLIENT_IP')?: getenv('HTTP_X_FORWARDED_FOR')?: getenv('HTTP_X_FORWARDED')?: getenv('HTTP_FORWARDED_FOR')?: getenv('HTTP_FORWARDED')?: getenv('REMOTE_ADDR'); echo $ip;
Output
3.226.76.98or3.226.76.98
Explanation
The getenv() is used to get the value of an environment variable in PHP $_SERVER is an array contains server variables created by the web server.