{"id":197,"date":"2017-03-14T07:18:36","date_gmt":"2017-03-14T07:18:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=197"},"modified":"2017-10-15T14:15:11","modified_gmt":"2017-10-15T08:45:11","slug":"get-client-ip-address-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/get-client-ip-address-php\/","title":{"rendered":"[Solved \u201310 Answers] How to get the client IP address in PHP"},"content":{"rendered":"<p><strong><span class=\"label label-warning\">PROBLEM :<\/span><\/strong><\/p>\n<p>How can we able to get the client IP address using PHP?<\/p>\n<ul>\n<li>We want to keep record of the user who logged into our website through IP address.<\/li>\n<\/ul>\n<p><span class=\"label label-info\">SOLUTION 1:\u00a0<\/span><\/p>\n<ul>\n<li>The simplest way is remote address server super global. But this is not the best way to do is using different server super global functions,\u00a0http client IP\u00a0and\u00a0http x forwarded for,\u00a0remote address.<\/li>\n<li>By using these three functions we can get correct ip address of a client or computer who is accessing our website.<\/li>\n<li>Simply use the below code to\u00a0get client IP address in php.<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%2F%2F%20Get%20the%20client%20IP%20address%0A%24ip%C2%A0%3D%C2%A0%24_SERVER%5B\u2019REMOTE_ADDR%E2%80%98%5D%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>we can use different server super global functions to check valid IP address.(http client IP\u00a0and\u00a0http x forwarded for,\u00a0remote address)<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24http_client_ip%20%3D%20%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D%3B%20%2F%2FInternet%20ip%20address%0A%24http_x_forwarded_for%20%3D%20%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D%3B%20%2F%2Fchecking%20for%20proxy%20server%0A%24remote_addr%20%3D%20%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D%3B%20%2F%2F%0Aif(!empty(%24http_client_ip))%7B%0A%24ip%20%3D%20%24http_client_ip%3B%0A%7Delseif(!empty(%24http_x_forwarded_for))%7B%0A%24ip%20%3D%20%24http_x_forwarded_for%3B%0A%7Delse%7B%0A%24ip%20%3D%20%24remote_addr%3B%0A%7D%0Aecho%20%24ip%3B%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span class=\"label label-info\">SOLUTION 2:\u00a0<\/span><\/p>\n<p>Sample code:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dif%20(!empty(%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D))%0A%20%7B%20%24ip%20%3D%20%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D%3B%20%0A%7D%0A%20elseif%20(!empty(%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D))%20%0A%24ip%20%3D%20%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D%3B%20%0A%7D%0A%20else%0A%20%7B%20%20%0A%24ip%20%3D%20%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D%3B%0A%20%7D%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Note: Using the above code has security implications. The client can set all HTTP header information (ie. $_SERVER[\u2018HTTP_\u2026) to any arbitrary value it wants. As such it\u2019s far more reliable to use $_SERVER[\u2018REMOTE_ADDR\u2019], as this cannot be set by the user.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><span class=\"label label-info\">SOLUTION 3:\u00a0<\/span><\/p>\n<p>Try this :<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201decho%20%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span class=\"label label-info\">SOLUTION 4:\u00a0<\/span><\/p>\n<p>Sample code:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24ip%20%3D%20%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D%3F%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D%3A(%24_SERVER%5B\u2019HTTP_X_FORWARDE%E2%80%8C%E2%80%8BD_FOR\u2019%5D%3F%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D%3A%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D)%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Here is a shorter version that uses the elvis operator<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D%3F%3A(%24_SERVER%5B\u2019HTTP_X_FORWARDE%E2%80%8C%E2%80%8BD_FOR\u2019%5D%3F%3A%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D)%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span class=\"label label-info\">SOLUTION 5:\u00a0<\/span><\/p>\n<ul>\n<li>This is the method validates an IPv4 input:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%2F%2F%20Get%20user%20IP%20address%20%0Aif%20(%20isset(%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D)%20%26%26%20!%20empty(%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D))%20%7B%0A%20%24ip%20%3D%20%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D%3B%20%0A%7D%0A%20elseif%20(%20isset(%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D)%20%26%26%20!%20empty(%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D))%0A%20%7B%0A%20%24ip%20%3D%20%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D%3B%0A%20%7D%20else%20%7B%0A%20%24ip%20%3D%20(isset(%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D))%20%3F%20%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D%20%3A%20\u20190.0.0.0\u2019%3B%20%0A%7D%0A%20%24ip%20%3D%20filter_var(%24ip%2C%20FILTER_VALIDATE_IP)%3B%20%0A%24ip%20%3D%20(%24ip%20%3D%3D%3D%20false)%20%3F%20\u20190.0.0.0\u2019%20%3A%20%24ip%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span class=\"label label-info\">SOLUTION 6:\u00a0<\/span><\/p>\n<ul>\n<li>Another method of implementation:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24ip%20%3D%20%22%22%3B%20%0Aif%20(!empty(%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D))%20%7B%0A%20%2F%2Fcheck%20for%20ip%20from%20share%20internet%20%24ip%20%3D%20%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D%3B%20%0A%7D%20elseif%20(!empty(%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D))%20%0A%7B%20%0A%2F%2F%20Check%20for%20the%20Proxy%20User%20%0A%24ip%20%3D%20%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D%3B%20%0A%7D%20else%0A%20%7B%20%0A%24ip%20%3D%20%24_SERVER%5B%22REMOTE_ADDR%22%5D%3B%0A%20%7D%20%0Aecho%20%24ip%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><span class=\"label label-info\">SOLUTION 7:\u00a0<\/span><\/p>\n<p>There are different type of users behind the Internet, So we want to catch the IP address from different potions. They are,<\/p>\n<p>1.\u00a0<strong>$_SERVER[\u2018REMOTE_ADDR\u2019]<\/strong>\u00a0\u2013 It contains the real IP address of the client. This is the most reliable value we can able to find from the user.<\/p>\n<p>2.\u00a0<strong>$_SERVER[\u2018REMOTE_HOST\u2019]\u00a0<\/strong>\u2013 It will fetch the Host name from which the user is viewing the current page. But for this script to work, Hostname Lookups On inside httpd.conf must be configured.<\/p>\n<p>3.\u00a0<strong>$_SERVER[\u2018HTTP_CLIENT_IP\u2019]<\/strong>\u00a0\u2013 It will fetch the IP address when user is from Shared Internet services.<\/p>\n<p>4.\u00a0<strong>$_SERVER[\u2018HTTP_X_FORWARDED_FOR\u2019]<\/strong>\u00a0\u2013 It will fetch the IP address from the user when he is behind the proxy<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%2F%2F%20Function%20to%20get%20the%20user%20IP%20address%20%0Afunction%20getUserIP()%20%7B%20%24ipaddress%20%3D%20\u201d%3B%20%0Aif%20(isset(%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B\u2019HTTP_CLIENT_IP\u2019%5D%3B%0Aelse%20if(isset(%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B\u2019HTTP_X_FORWARDED_FOR\u2019%5D%3B%0Aelse%20if(isset(%24_SERVER%5B\u2019HTTP_X_FORWARDED\u2019%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B\u2019HTTP_X_FORWARDED\u2019%5D%3B%0Aelse%20if(isset(%24_SERVER%5B\u2019HTTP_X_CLUSTER_CLIENT_IP\u2019%5D))%20%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B\u2019HTTP_X_CLUSTER_CLIENT_IP\u2019%5D%3B%0Aelse%20if(isset(%24_SERVER%5B\u2019HTTP_FORWARDED_FOR\u2019%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B\u2019HTTP_FORWARDED_FOR\u2019%5D%3B%20%0Aelse%20if(isset(%24_SERVER%5B\u2019HTTP_FORWARDED\u2019%5D))%0A%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B\u2019HTTP_FORWARDED\u2019%5D%3B%20%0Aelse%20if(isset(%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D%3B%20%0Aelse%20%0A%20%20%20%20%20%24ipaddress%20%3D%20\u2019UNKNOWN\u2019%3B%20%0Areturn%20%24ipaddress%3B%0A%20%7D%0A%0B%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span class=\"label label-info\">SOLUTION 8:\u00a0<\/span><\/p>\n<p>using the GLOBAL variable named as <strong>$_SERVER.<\/strong><\/p>\n<p>the <strong>$_SERVER<\/strong> is an array which has an attribute names <strong>REMOTE_ADDR.<\/strong><\/p>\n<p>Just assign it like this<strong> $userIp = $_SERVER[\u2018REMOTE_ADDR\u2019];<\/strong><\/p>\n<p>or use it directly like echo <strong>$_SERVER[\u2018REMOTE_ADDR\u2019]; or echo ($_SERVER[\u2018REMOTE_ADDR\u2019]);<\/strong><\/p>\n[ad type=\u201dbanner\u201d]\n<p><span class=\"label label-info\">SOLUTION 9:\u00a0<\/span><\/p>\n<ul>\n<li>We can able to use $_SERVER[\u2018REMOTE_ADDR\u2019]; to get client IP address and if we need more information about a user, Use below sample code:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%20%24ip%3D\u20190.0.0.0\u2019%3B%0A%20%24ip%3D%24_SERVER%5B\u2019REMOTE_ADDR\u2019%5D%3B%20%0A%24clientDetails%20%3D%20json_decode(file_get_contents(%22http%3A%2F%2Fipinfo.io%2F%24ip%2Fjson%22))%3B%20%0Aecho%20%22You\u2019re%20logged%20in%20from%3A%20%3Cb%3E%22%20.%20%24clientDetails-%3Ecountry%20.%20%22%3C%2Fb%3E%22%3B%20%0A%3F%3E%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Client\u2019s more specific info goes in $clientDetails.\u000bWe can able to fetch json items stored in $clientDetails variable in this way: $clientDetails->PostalCode\/hostname\/region\/loc..<\/li>\n<\/ul>\n<p><span class=\"label label-info\">SOLUTION 10:\u00a0<\/span><\/p>\n<ul>\n<li>Here is the another solution to get the ip address<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201dfunction%20getClientIP()%20%7B%20%0Aif%20(isset(%24_SERVER))%0A%20%7B%0A%20if%20(isset(%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D))%20%0A%20%20%20%20return%20%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D%3B%20%0A%0Aif%20(isset(%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D))%0A%20%20%20return%20%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D%3B%20%0A%0A%20%20%20return%20%24_SERVER%5B%22REMOTE_ADDR%22%5D%3B%0A%20%7D%0A%20if%20(getenv(\u2018HTTP_X_FORWARDED_FOR\u2019))%20%0A%20%20%20%20return%20getenv(\u2018HTTP_X_FORWARDED_FOR\u2019)%3B%0A%0A%20if%20(getenv(\u2018HTTP_CLIENT_IP\u2019))%20%0A%20%20%20%20return%20getenv(\u2018HTTP_CLIENT_IP\u2019)%3B%0A%0A%20%20%20%20%20return%20getenv(\u2018REMOTE_ADDR\u2019)%3B%20%0A%7D%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>[Solved \u201310 Answers] PHP &#8211; How to get the client IP address in PHP? &#8211; How can we able to get the client IP address using PHP? We want to keep record of<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83482,25],"tags":[418,423,420,413,422,417,414,424,421,419,416,415,425],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-ip-address","category-php","tag-_serverserver_addr-is-undefined-on-php-over-plesk","tag-access-ip-address-of-client-machine-in-php","tag-get-ip-and-mac-address-of-client-using-php-or-javascript","tag-get-the-client-ip-address-using-php","tag-how-can-i-get-the-client-ip-address-in-php","tag-how-do-i-add-client-ip-address-to-database-while-registering","tag-how-do-i-find-a-users-ip-address-with-php","tag-how-to-check-valid-ip-addresses-with-php","tag-how-to-get-global-ip-address-of-the-client","tag-how-to-get-ip-address-of-client-machine-in-mysql-database","tag-how-to-get-my-site-visitors-ip-address-in-php","tag-how-to-get-real-ip-of-user-using-php","tag-how-to-get-the-ip-addresses-of-clients-using-php"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}