{"id":1164,"date":"2022-07-17T11:12:49","date_gmt":"2022-07-17T05:42:49","guid":{"rendered":"https:\/\/lookouthost.com\/?p=1164"},"modified":"2022-07-17T11:50:59","modified_gmt":"2022-07-17T06:20:59","slug":"how-to-get-visitors-ip-in-php","status":"publish","type":"post","link":"https:\/\/lookouthost.com\/blog\/how-to-get-visitors-ip-in-php\/","title":{"rendered":"How To Get Visitor&#8217;s IP in PHP"},"content":{"rendered":"<p>The simplest way to get the visitor\u2019s\/client\u2019s&nbsp;IP address&nbsp;is using the&nbsp;<code>$_SERVER['REMOTE_ADDR']<\/code>&nbsp;or&nbsp;<code>$_SERVER['REMOTE_HOST']<\/code>&nbsp;variables.<\/p><p>Sometimes this does not return the correct IP address of the visitor. Therefore some other server variables are also being used in below exmple to get the correct IP address.<\/p><p>getenv() function is used to get the value of an environment variable in PHP.<\/p><pre class=\"wp-block-code\"><code>\/\/ Function to get the client IP address\nfunction get_client_ip() {\n    $ipaddress = '';\n    if (getenv('HTTP_CLIENT_IP'))\n        $ipaddress = getenv('HTTP_CLIENT_IP');\n    else if(getenv('HTTP_X_FORWARDED_FOR'))\n        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');\n    else if(getenv('HTTP_X_FORWARDED'))\n        $ipaddress = getenv('HTTP_X_FORWARDED');\n    else if(getenv('HTTP_FORWARDED_FOR'))\n        $ipaddress = getenv('HTTP_FORWARDED_FOR');\n    else if(getenv('HTTP_FORWARDED'))\n       $ipaddress = getenv('HTTP_FORWARDED');\n    else if(getenv('REMOTE_ADDR'))\n        $ipaddress = getenv('REMOTE_ADDR');\n    else\n        $ipaddress = 'UNKNOWN';\n    return $ipaddress;\n}\n$ip = 'get_client_ip';\necho get_client_ip();\n?&gt;\n\n<\/code><\/pre><figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"394\" src=\"https:\/\/lookouthost.com\/wp-content\/uploads\/2022\/07\/detectip-1024x394.png\" alt=\"D\" class=\"wp-image-1165\" srcset=\"https:\/\/lookouthost.com\/blog\/wp-content\/uploads\/2022\/07\/detectip-1024x394.png 1024w, https:\/\/lookouthost.com\/blog\/wp-content\/uploads\/2022\/07\/detectip-600x231.png 600w, https:\/\/lookouthost.com\/blog\/wp-content\/uploads\/2022\/07\/detectip-300x115.png 300w, https:\/\/lookouthost.com\/blog\/wp-content\/uploads\/2022\/07\/detectip-768x295.png 768w, https:\/\/lookouthost.com\/blog\/wp-content\/uploads\/2022\/07\/detectip.png 1360w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>","protected":false},"excerpt":{"rendered":"<p>Detect Visitor&#8217;s IP in PHP<\/p>\n","protected":false},"author":1,"featured_media":1161,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/posts\/1164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/comments?post=1164"}],"version-history":[{"count":0,"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/media\/1161"}],"wp:attachment":[{"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/media?parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/categories?post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lookouthost.com\/blog\/wp-json\/wp\/v2\/tags?post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}