$ch = curl_init();
$url = "https://localhost/target_ip.php";
$header = array(
'CLIENT-IP: 58.68.44.61',
'X-FORWARDED-FOR: 58.68.44.61',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page_content = curl_exec($ch);
curl_close($ch);
echo $page_content;
<?php
echo getenv('HTTP_CLIENT_IP');
echo getenv('HTTP_X_FORWARDED_FOR');
echo getenv('REMOTE_ADDR');
?>
輸出:
58.68.44.61
58.68.44.61
127.0.0.1
curl_setopt($ch, CURLOPT_REFERER, "https://www.jb51.net/");
function getClientIp() {
if (!empty($_SERVER["HTTP_CLIENT_IP"])) return $_SERVER["HTTP_CLIENT_IP"];
if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) return $_SERVER["HTTP_X_FORWARDED_FOR"];
return $_SERVER["REMOTE_ADDR"];
}
上述函數**極易被繞過**,因標頭完全可控。
X-Forwarded-For 做權限判斷。REMOTE_ADDR(真實 TCP 連接 IP)與代理信任鏈驗證。real_ip_module 從可信代理頭提取真實 IP。