{"id":6795,"date":"2018-11-05T16:55:17","date_gmt":"2018-11-05T21:55:17","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=6795"},"modified":"2024-02-20T16:51:02","modified_gmt":"2024-02-20T13:51:02","slug":"getting-client-ip-address-php","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/","title":{"rendered":"How to get the Right Client IP Address in PHP"},"content":{"rendered":"\n
If you have been a web developer for quite some time and are in the habit of sifting through your visitor and access logs, depending on your situation you may tend to notice that you do not always get the right client IP address from your users.<\/p>\n\n\n\n
This can be caused by various issues like being behind a proxy or public reverse-proxy.<\/p>\n\n\n\n
The most compelling reasons would fall under security purposes. Others are for validation, geo-filtering, spam prevention, and more. The easiest and most straightforward way to get the visitors IP address in PHP<\/a> would be to use the following:<\/p>\n\n\n\n The problem is this call is not always reliable as it may provide the wrong address. For example, if the client is behind a proxy, then this call would most likely return the IP address of the proxy and not the client.<\/p>\n\n\n\n So what is a better solution? We can use PHP’s The following code is interchangeable between the two.<\/p>\n\n\n\n Here is a breakdown of what these variables contain:<\/p>\n\n\n\n 1. $_SERVER[‘REMOTE_ADDR’]<\/strong> – This contains the IP address of the client. That is may not be the real address because of external factors like proxy servers etc unless you are using HTTPS<\/a>.<\/p>\n\n\n\n\/\/ Get the visitors IP address\n $ip_address = $_SERVER['REMOTE_ADDR'];<\/code><\/pre>\n\n\n\n
getenv()<\/code> function or
$_SERVER<\/code> to iterate through the various variables. The difference here is that
getenv()<\/code> returns the PHP environment variable while
$_SERVER<\/code> returns the web server variables.<\/p>\n\n\n\n
function getClientIpWithServer() {\n $ipaddress = '';\n if (isset($_SERVER['HTTP_CLIENT_IP']))\n $ipaddress = $_SERVER['HTTP_CLIENT_IP'];\n else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))\n $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];\n else if(isset($_SERVER['HTTP_X_FORWARDED']))\n $ipaddress = $_SERVER['HTTP_X_FORWARDED'];\n else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))\n $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];\n else if(isset($_SERVER['HTTP_FORWARDED_FOR']))\n $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];\n else if(isset($_SERVER['HTTP_FORWARDED']))\n $ipaddress = $_SERVER['HTTP_FORWARDED'];\n else if(isset($_SERVER['REMOTE_ADDR']))\n $ipaddress = $_SERVER['REMOTE_ADDR'];\n else\n $ipaddress = 'UNKNOWN';\n return $ipaddress;\n}\n\nfunction getClientIpWithGetEnv() {\n $ipaddress = '';\n if (isset(getenv['HTTP_CLIENT_IP']))\n $ipaddress = getenv['HTTP_CLIENT_IP'];\n else if(isset(getenv['HTTP_X_FORWARDED_FOR']))\n $ipaddress = getenv['HTTP_X_FORWARDED_FOR'];\n else if(isset(getenv['HTTP_X_FORWARDED']))\n $ipaddress = getenv['HTTP_X_FORWARDED'];\n else if(isset(getenv['HTTP_X_CLUSTER_CLIENT_IP']))\n $ipaddress = getenv['HTTP_X_CLUSTER_CLIENT_IP'];\n else if(isset(getenv['HTTP_FORWARDED_FOR']))\n $ipaddress = getenv['HTTP_FORWARDED_FOR'];\n else if(isset(getenv['HTTP_FORWARDED']))\n $ipaddress = getenv['HTTP_FORWARDED'];\n else if(isset(getenv['REMOTE_ADDR']))\n $ipaddress = getenv['REMOTE_ADDR'];\n else\n $ipaddress = 'UNKNOWN';\n return $ipaddress;\n}<\/code><\/pre>\n\n\n\n