You, probably, are looking for this: $_SERVER["HTTP_CF_CONNECTING_IP"]
or
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ?? $_SERVER["X-Forwarded-For"] ?? $_SERVER['REMOTE_ADDR'];
As cloudflare proxies requests through it's own servers, $_SERVER['REMOTE_ADDR']
shows ip address of one of cloudflare servers, not the real user.
Cloudflare adds several additional headers to requests and responses. The HTTP_CF_CONNECTING_IP
one has real user ip.
with nginx config
If you want to solve problem as system administrator, check out nginx solution. It uses ngx_http_realip_module.
with yii2
If you are using Yii2, modify your web-config (default is config/web.php
) and add ipHeaders param to request as follows:
'request' => [
'ipHeaders' => [
'CF-Connecting-IP',
'X-Forwarded-For',
],
],
X-Forwarded-For
is default header used by Yii, I recommend to leave it as is.