打印一下$_SERVER

<?php
echo json_encode($_SERVER, JSON_UNESCAPED_UNICODE);
?>

有这几项值得注意

"HTTP_X_FORWARDED_FOR":"[YOUR_IP]",
"HTTP_CF_IPCOUNTRY":"[YOUR_COUNTRY]",
"HTTP_CF_CONNECTING_IP":"[YOUR_IP]",
"HTTP_CDN_LOOP":"cloudflare",

`$_SERVER["HTTP_X_FORWARDED_FOR"]` 就是客户端的真实IP地址

另外不支持IPv6的服务器也可以通过 $_SERVER["HTTP_X_FORWARDED_FOR"] 来获取客户端的IPv6地址

自动根据情况获取IP:

$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != "" ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];