我正在使用以下PHP代码获取访问者的IP地址:
<?php echo $_SERVER['REMOTE_ADDR']; ?>
但是, 当 访客 使用Proxy时 ,我无法从访客那里获得真实的IP地址。在这种情况下,是否可以获取访客的IP地址?
试试这个PHP代码。
<?PHP function getUserIP() { // Get real visitor IP behind CloudFlare network if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) { $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; } $client = @$_SERVER['HTTP_CLIENT_IP']; $forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; $remote = $_SERVER['REMOTE_ADDR']; if(filter_var($client, FILTER_VALIDATE_IP)) { $ip = $client; } elseif(filter_var($forward, FILTER_VALIDATE_IP)) { $ip = $forward; } else { $ip = $remote; } return $ip; } $user_ip = getUserIP(); echo $user_ip; // Output IP address [Ex: 177.87.193.134] ?>