{"id":39,"date":"2013-07-19T13:03:57","date_gmt":"2013-07-19T07:03:57","guid":{"rendered":"http:\/\/sarojroy.com\/?p=39"},"modified":"2013-07-19T13:26:47","modified_gmt":"2013-07-19T07:26:47","slug":"most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php","status":"publish","type":"post","link":"http:\/\/sarojroy.com\/blog\/2013\/07\/19\/most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php\/","title":{"rendered":"Most accurate way to retrieve a user&#8217;s correct IP address in PHP"},"content":{"rendered":"<p>Sometimes it is required to determine cilent&#8217;s IP address in PHP applications. It is straight forward to get it from\u00a0REMOTE_ADDR headers. However, it becomes tricky if there are proxies in-between.<\/p>\n<p>Some application frameworks like Symfony has built-in method to get it. However, if you need to determine the IP address on your own, here is the the solution:<\/p>\n<pre class=\"brush: php; gutter: true\">function get_ip_address(){\r\n\t$ipHeaders = array(\r\n        &#039;HTTP_CLIENT_IP&#039;, \r\n        &#039;HTTP_X_FORWARDED_FOR&#039;, \r\n        &#039;HTTP_X_FORWARDED&#039;, \r\n        &#039;HTTP_X_CLUSTER_CLIENT_IP&#039;, \r\n        &#039;HTTP_FORWARDED_FOR&#039;, \r\n        &#039;HTTP_FORWARDED&#039;,\r\n        &#039;REMOTE_ADDR&#039;\r\n\t);\r\n    foreach ($ipHeaders as $key){\r\n        if (array_key_exists($key, $_SERVER) === true){\r\n            foreach (explode(&#039;,&#039;, $_SERVER[$key]) as $ip){\r\n                \/\/ just to be safe\r\n                $ip = trim($ip); \r\n                \/\/filter the ip before returning\r\n                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){\r\n                    return $ip;\r\n                }\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>WARNING:\u00a0<\/strong>REMOTE_ADDR still represents the most reliable source of an IP address. The other $_SERVER variables can be spoofed by a remote client very easily.<\/p>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">Ref: <a title=\"Stakeoverflow\" href=\"http:\/\/stackoverflow.com\/questions\/1634782\/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php\" target=\"_blank\">Stakeoverflow<\/a><br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it is required to determine cilent&#8217;s IP address in PHP applications. It is straight forward to get it from\u00a0REMOTE_ADDR headers. However, it becomes tricky if there are proxies in-between. Some application frameworks like Symfony has built-in method to get it. However, if you need to determine the IP address on your own, here is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[16,24],"_links":{"self":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts\/39"}],"collection":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":12,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":47,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions\/47"}],"wp:attachment":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}