{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">\/\/ Get the client IP address<br\/>$ip\u00a0=\u00a0$_SERVER[&#039;REMOTE_ADDR\u2018]<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$http_client_ip = $_SERVER[&#039;HTTP_CLIENT_IP&#039;]; \/\/Internet ip address<br\/>$http_x_forwarded_for = $_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;]; \/\/checking for proxy server<br\/>$remote_addr = $_SERVER[&#039;REMOTE_ADDR&#039;]; \/\/<br\/>if(!empty($http_client_ip)){<br\/>$ip = $http_client_ip;<br\/>}elseif(!empty($http_x_forwarded_for)){<br\/>$ip = $http_x_forwarded_for;<br\/>}else{<br\/>$ip = $remote_addr;<br\/>}<br\/>echo $ip;<\/code><\/pre> <\/div>\n<p><span class=\"label label-info\">SOLUTION 2:\u00a0<\/span><\/p>\n<p>Sample code:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">if (!empty($_SERVER[&#039;HTTP_CLIENT_IP&#039;]))<br\/> { $ip = $_SERVER[&#039;HTTP_CLIENT_IP&#039;]; <br\/>}<br\/> elseif (!empty($_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;])) <br\/>$ip = $_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;]; <br\/>}<br\/> else<br\/> {  <br\/>$ip = $_SERVER[&#039;REMOTE_ADDR&#039;];<br\/> } <\/code><\/pre> <\/div>\n<p>Note: Using the above code has security implications. The client can set all HTTP header information (ie. $_SERVER[&#8216;HTTP_&#8230;) to any arbitrary value it wants. As such it&#8217;s far more reliable to use $_SERVER[&#8216;REMOTE_ADDR&#8217;], as this cannot be set by the user.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><span class=\"label label-info\">SOLUTION 3:\u00a0<\/span><\/p>\n<p>Try this :<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">echo $_SERVER[&#039;REMOTE_ADDR&#039;]; <\/code><\/pre> <\/div>\n<p><span class=\"label label-info\">SOLUTION 4:\u00a0<\/span><\/p>\n<p>Sample code:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$ip = $_SERVER[&#039;HTTP_CLIENT_IP&#039;]?$_SERVER[&#039;HTTP_CLIENT_IP&#039;]:($_SERVER[&#039;HTTP_X_FORWARDE\u200c\u200bD_FOR&#039;]?$_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;]:$_SERVER[&#039;REMOTE_ADDR&#039;]); <\/code><\/pre> <\/div>\n<p>Here is a shorter version that uses the elvis operator<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$_SERVER[&#039;HTTP_CLIENT_IP&#039;]?:($_SERVER[&#039;HTTP_X_FORWARDE\u200c\u200bD_FOR&#039;]?:$_SERVER[&#039;REMOTE_ADDR&#039;]); <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">\/\/ Get user IP address <br\/>if ( isset($_SERVER[&#039;HTTP_CLIENT_IP&#039;]) &amp;&amp; ! empty($_SERVER[&#039;HTTP_CLIENT_IP&#039;])) {<br\/> $ip = $_SERVER[&#039;HTTP_CLIENT_IP&#039;]; <br\/>}<br\/> elseif ( isset($_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;]) &amp;&amp; ! empty($_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;]))<br\/> {<br\/> $ip = $_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;];<br\/> } else {<br\/> $ip = (isset($_SERVER[&#039;REMOTE_ADDR&#039;])) ? $_SERVER[&#039;REMOTE_ADDR&#039;] : &#039;0.0.0.0&#039;; <br\/>}<br\/> $ip = filter_var($ip, FILTER_VALIDATE_IP); <br\/>$ip = ($ip === false) ? &#039;0.0.0.0&#039; : $ip; <\/code><\/pre> <\/div>\n<p><span class=\"label label-info\">SOLUTION 6:\u00a0<\/span><\/p>\n<ul>\n<li>Another method of implementation:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$ip = &quot;&quot;; <br\/>if (!empty($_SERVER[&quot;HTTP_CLIENT_IP&quot;])) {<br\/> \/\/check for ip from share internet $ip = $_SERVER[&quot;HTTP_CLIENT_IP&quot;]; <br\/>} elseif (!empty($_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;])) <br\/>{ <br\/>\/\/ Check for the Proxy User <br\/>$ip = $_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;]; <br\/>} else<br\/> { <br\/>$ip = $_SERVER[&quot;REMOTE_ADDR&quot;];<br\/> } <br\/>echo $ip; <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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[&#8216;REMOTE_ADDR&#8217;]<\/strong>\u00a0&#8211; 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[&#8216;REMOTE_HOST&#8217;]\u00a0<\/strong>&#8211; 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[&#8216;HTTP_CLIENT_IP&#8217;]<\/strong>\u00a0&#8211; It will fetch the IP address when user is from Shared Internet services.<\/p>\n<p>4.\u00a0<strong>$_SERVER[&#8216;HTTP_X_FORWARDED_FOR&#8217;]<\/strong>\u00a0&#8211; It will fetch the IP address from the user when he is behind the proxy<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">\/\/ Function to get the user IP address <br\/>function getUserIP() { $ipaddress = &#039;&#039;; <br\/>if (isset($_SERVER[&#039;HTTP_CLIENT_IP&#039;]))<br\/>     $ipaddress = $_SERVER[&#039;HTTP_CLIENT_IP&#039;];<br\/>else if(isset($_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;]))<br\/>     $ipaddress = $_SERVER[&#039;HTTP_X_FORWARDED_FOR&#039;];<br\/>else if(isset($_SERVER[&#039;HTTP_X_FORWARDED&#039;]))<br\/>     $ipaddress = $_SERVER[&#039;HTTP_X_FORWARDED&#039;];<br\/>else if(isset($_SERVER[&#039;HTTP_X_CLUSTER_CLIENT_IP&#039;])) <br\/>     $ipaddress = $_SERVER[&#039;HTTP_X_CLUSTER_CLIENT_IP&#039;];<br\/>else if(isset($_SERVER[&#039;HTTP_FORWARDED_FOR&#039;]))<br\/>     $ipaddress = $_SERVER[&#039;HTTP_FORWARDED_FOR&#039;]; <br\/>else if(isset($_SERVER[&#039;HTTP_FORWARDED&#039;]))<br\/>    $ipaddress = $_SERVER[&#039;HTTP_FORWARDED&#039;]; <br\/>else if(isset($_SERVER[&#039;REMOTE_ADDR&#039;]))<br\/>     $ipaddress = $_SERVER[&#039;REMOTE_ADDR&#039;]; <br\/>else <br\/>     $ipaddress = &#039;UNKNOWN&#039;; <br\/>return $ipaddress;<br\/> }<br\/>\u000b<\/code><\/pre> <\/div>\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[&#8216;REMOTE_ADDR&#8217;];<\/strong><\/p>\n<p>or use it directly like echo <strong>$_SERVER[&#8216;REMOTE_ADDR&#8217;]; or echo ($_SERVER[&#8216;REMOTE_ADDR&#8217;]);<\/strong><\/p>\n[ad type=&#8221;banner&#8221;]\n<p><span class=\"label label-info\">SOLUTION 9:\u00a0<\/span><\/p>\n<ul>\n<li>We can able to use $_SERVER[&#8216;REMOTE_ADDR&#8217;]; to get client IP address and if we need more information about a user, Use below sample code:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/> $ip=&#039;0.0.0.0&#039;;<br\/> $ip=$_SERVER[&#039;REMOTE_ADDR&#039;]; <br\/>$clientDetails = json_decode(file_get_contents(&quot;http:\/\/ipinfo.io\/$ip\/json&quot;)); <br\/>echo &quot;You&#039;re logged in from: &lt;b&gt;&quot; . $clientDetails-&gt;country . &quot;&lt;\/b&gt;&quot;; <br\/>?&gt; <\/code><\/pre> <\/div>\n<ul>\n<li>Client&#8217;s more specific info goes in $clientDetails.\u000bWe can able to fetch json items stored in $clientDetails variable in this way: $clientDetails-&gt;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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">function getClientIP() { <br\/>if (isset($_SERVER))<br\/> {<br\/> if (isset($_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;])) <br\/>    return $_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;]; <br\/><br\/>if (isset($_SERVER[&quot;HTTP_CLIENT_IP&quot;]))<br\/>   return $_SERVER[&quot;HTTP_CLIENT_IP&quot;]; <br\/><br\/>   return $_SERVER[&quot;REMOTE_ADDR&quot;];<br\/> }<br\/> if (getenv(&#039;HTTP_X_FORWARDED_FOR&#039;)) <br\/>    return getenv(&#039;HTTP_X_FORWARDED_FOR&#039;);<br\/><br\/> if (getenv(&#039;HTTP_CLIENT_IP&#039;)) <br\/>    return getenv(&#039;HTTP_CLIENT_IP&#039;);<br\/><br\/>     return getenv(&#039;REMOTE_ADDR&#039;); <br\/>} <\/code><\/pre> <\/div>\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}]}}