{"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

Why Would I Need the Right Client IP Address?<\/h2>\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

\/\/ Get the visitors IP address\n $ip_address = $_SERVER['REMOTE_ADDR'];<\/code><\/pre>\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 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

The following code is interchangeable between the two.<\/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

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

2. $_SERVER[‘HTTP_CLIENT_IP’]<\/strong> – This will fetch the IP address where the user is accessing from shared Internet<\/a> services.<\/p>\n\n\n\n

3. $_SERVER[‘HTTP_X_FORWARDED_FOR’]<\/strong> – This will fetch the IP address from the users when they are behind a proxy. It can sometimes return an internal or local IP address, which may not be useful in most circumstances. If forwarded from multiple IP addresses it can return a comma-separated list of addresses.<\/p>\n\n\n\n

There you have it. That is the quick way to retrieve the correct client IP address to use in your code.<\/p>\n","protected":false},"excerpt":{"rendered":"

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…<\/p>\n","protected":false},"author":1,"featured_media":6796,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,27,16],"tags":[58,320,452,471,531,543,544,635,637,638],"yoast_head":"\nHere's the Right way to get the Client IP Address in PHP<\/title>\n<meta name=\"description\" content=\"This is how to use a combination of REMOTE_ADDR, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR to get the right client IP adress in PHP\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Here's the Right way to get the Client IP Address in PHP\" \/>\n<meta property=\"og:description\" content=\"This is how to use a combination of REMOTE_ADDR, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR to get the right client IP adress in PHP\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-05T21:55:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-20T13:51:02+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How to get the Right Client IP Address in PHP\",\"datePublished\":\"2018-11-05T21:55:17+00:00\",\"dateModified\":\"2024-02-20T13:51:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/\"},\"wordCount\":340,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg\",\"keywords\":[\"Apache\",\"Internet\",\"PHP\",\"Programming\",\"Server\",\"Software Design\",\"Software development\",\"Web\",\"Web Design\",\"Web Development\"],\"articleSection\":[\"Articles\",\"Guides\",\"Programming\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/\",\"url\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/\",\"name\":\"Here's the Right way to get the Client IP Address in PHP\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg\",\"datePublished\":\"2018-11-05T21:55:17+00:00\",\"dateModified\":\"2024-02-20T13:51:02+00:00\",\"description\":\"This is how to use a combination of REMOTE_ADDR, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR to get the right client IP adress in PHP\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Client IP Address\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get the Right Client IP Address in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/twitter.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Here's the Right way to get the Client IP Address in PHP","description":"This is how to use a combination of REMOTE_ADDR, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR to get the right client IP adress in PHP","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/","og_locale":"en_US","og_type":"article","og_title":"Here's the Right way to get the Client IP Address in PHP","og_description":"This is how to use a combination of REMOTE_ADDR, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR to get the right client IP adress in PHP","og_url":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2018-11-05T21:55:17+00:00","article_modified_time":"2024-02-20T13:51:02+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How to get the Right Client IP Address in PHP","datePublished":"2018-11-05T21:55:17+00:00","dateModified":"2024-02-20T13:51:02+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/"},"wordCount":340,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg","keywords":["Apache","Internet","PHP","Programming","Server","Software Design","Software development","Web","Web Design","Web Development"],"articleSection":["Articles","Guides","Programming","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/","url":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/","name":"Here's the Right way to get the Client IP Address in PHP","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg","datePublished":"2018-11-05T21:55:17+00:00","dateModified":"2024-02-20T13:51:02+00:00","description":"This is how to use a combination of REMOTE_ADDR, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR to get the right client IP adress in PHP","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/getting-client-ip-address-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2018\/11\/client-ip-address.jpg","width":1200,"height":630,"caption":"Client IP Address"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/getting-client-ip-address-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How to get the Right Client IP Address in PHP"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/twitter.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/6795"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=6795"}],"version-history":[{"count":2,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/6795\/revisions"}],"predecessor-version":[{"id":13583,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/6795\/revisions\/13583"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/6796"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=6795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=6795"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=6795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}