Cloudflare and visitors IP addresses

Cloudflare reverse proxy

Cloudflare very popular service, which provides many necessary and useful services. Maybe most popular service  is their reverse proxy, which allows you to put your site behind cloud caching proxy servers. This gives possibility to reduce load on the site and make content output more faster.

But you may also face the situation, when your visitors IP addresses will become “hidden” behind proxy servers addresses. In particular if your site code uses HTTP headers data for retrieval of IP address, then it will get the addresses of proxy servers and not the real addresses of visitors. For example, while using $_SERVER['REMOTE_ADDR'] in PHP. So if there is some functionality in your code which uses users IPs(e.g. analytics, statistics), then its data could be incorrect.

Fortunately Cloudflare servers send their additional headers and there is a CF-Connecting-IP header, which will show real source IP address. In PHP  it could be accessed by using $_SERVER[“HTTP_CF_CONNECTING_IP”] variable.

In addition Cloudflare can send another useful headers, for example CF-IPCountry ($_SERVER[“HTTP_CF_IPCOUNTRY”]).  It contains a two character value of visitor’s country .

 

More details:

More details about proxy servers passing headers could be found on the official site in support area here: https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers-

 

Напишите комментарий

Your email address will not be published. Required fields are marked *