Not much, but in this blog post, you can expect the easiest way How to get CloudFlare Visitor IPs in PHP programming.
As a webmaster, we always want to improve the website performance with CDN, so that I decide to go with CloudFlare. But we usually meet the problem CloudFlare with website/forum spam what we can’t check spam via visitors IPs because we are under CloudFlare proxy server.
Very simple with this PHP code
1 2 3 4 5 | if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){ $ip = $_SERVER['HTTP_CF_CONNECTING_IP']; }else{ $ip=$_SERVER['REMOTE_ADDR']; } |
If you want to get the real IP value from cloudflare to your PHP source in the way what you don’t have to change anything in coding but the system still run base on the real visitors IPs, you can use this way:
1 2 3 | if(isset($_SERVER["HTTP_CF_CONNECTING_IP"])){ $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; } |
Very simple and easy to use but useful. Thanks for sharing the good way to get Ip from cloudflare proxy. It take me a lot of time before.
Sorry to comment again to ask about this line
$_SERVER[‘REMOTE_ADDR’] = $_SERVER[‘HTTP_CF_CONNECTING_IP’];
Is it official if we do like this? I mean when we do this, is it risk or easy to turn out any issue? Thanks
It ‘s just the way I usually use. Actually, it is not official, after doing that you will have no way to know the IP of the proxy but who want to know that, we just want to know the real IP and we send it to all of coding process what need to real IP.
So in the bottom line, your coding is fine to use.
Let ‘s override the default param in global like $_SERVER[‘REMOTE_ADDR’] = $_SERVER[‘HTTP_CF_CONNECTING_IP’];, I do it all of times and I didn’t meet any problem until now. Everything work well 🙂
It ‘s nice have a good free service like cloudflare and easy to code on their service too.
Good to know it ‘s too easy to get real ip in proxy :).
This is must know info for any programmer who work under cloudflare proxy. But it ‘s pity that the free service from cloudflare what doesn’t help any more. They just doing the DNS management.