Cloudflare is giving me the wrong IP when I use getenv(REMOTE_ADDR)

1583 views php

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?

$ip = getenv(REMOTE_ADDR);
mail("", "You got a visitor", "IP: ".$ip);

answered question

1 Answer


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 X-Forwarded-For and other useful headers Cloudflare generate to figure out the user's real origin.

Try again with $_SERVER['HTTP_X_FORWARDED_FOR'] instead of getenv(REMOTE_ADDR) and see what happens.

posted this

Have an answer?


Please login first before posting an answer.