php Quick Tips

$_SERVER[“REMOTE_ADDR”] not working with Cloudflare configuration

so You might want to get the user ip in a PHP website, and you have searched and found $_SERVER["REMOTE_ADDR"]is the answer.

But despite of everything you do, you get to a road block of getting the same IP address.

The problem is, cloudflare basically routes the traffic to your server, hence the same ip address every time, but the solution is, cloudflare also sends a custom header with the request HTTP_CF_CONNECTING_IP

so instead of doing $_SERVER[“REMOTE_ADDR”] you only have to do $_SERVER["HTTP_CF_CONNECTING_IP"]

Leave a Reply

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