How to determine visitor IP address on Cloudflare using PHP

If you have recently made a wise decision of moving your website to CloudFlare so that you can root out unwanted traffic to your website, avoid DDOS attacks and cache your website so that it loads quickly for all visitors of your website then you will need to make a few changes to your code.

The changes in code are only needed if you display/hide sections on your website based on the visitor IP and country. You will need replace all occurrences of $_SERVER[“REMOTE_ADDR”] with $_SERVER[“HTTP_CF_CONNECTING_IP”].

You can test this with the following code in PHP

// See what REMOTE_ADDR prints out
echo $_SERVER["REMOTE_ADDR"] . PHP_EOL;

// Correct IP of visitor now
echo $_SERVER["HTTP_CF_CONNECTING_IP"] . PHP_EOL;
Please follow and like us:
error

Be the first to comment

Leave a Reply

Your email address will not be published.