Search code examples
cloudflareipv4

Get visitors ipv4 behind cloudflare


I just have the problem that I want to get the visitors IP Address. Everythings fine but one user just give me the IPv6. This is the code I can give you, moreover I just had installed the cloudflare apache2 Mod.

 $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"] ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
 $ip = $_SERVER['REMOTE_ADDR'];

 if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
     $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
 }

Solution

  • The only way to do this would be to disable IPv6 support in CloudFlare by:

    1) In DNS removing all AAAA records (leaving only A records). 2) Going into the Network on the CloudFlare dashboard and turning off "IPv6 Compatibility".

    I would not recommend this though; you might soon find yourself with people unable to connect to your site. Instead it is a better idea to make sure your platform fully supports IPv6.