I have a test php code. Which essentially grabs the IP of someone and emails it to an address. I plan later on storing it in a database, but just for testing purposes I have put it as emailing me.
My issue is, I have cloudflare enabled so when I use getenv(REMOTE_ADDR)
it gives me cloudflare's IP rather than the actual visitors IP. Is there a way I can get the visitor's IP?
<?php
$ip = getenv(REMOTE_ADDR);
mail("email@domain.com", "You got a visitor", "IP: ".$ip);
?>
Of course it is, Cloudflare hides your server real address from the user by intermediating the connection (reverse Proxy), and at the same rate you see the proxy's IP accessing the page instead of the user's.
But they report the real IP through the header CF-Connecting-IP
and other useful headers Cloudflare generate to figure out the user's real origin.
Try again with $_SERVER['HTTP_CF_CONNECTING_IP']
instead of getenv(REMOTE_ADDR)
and see what happens.