PyProxy/IP-API/IP-test.php
2025-01-09 14:16:05 +08:00

46 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
function getRealIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
// 检查是否通过了HTTP客户端IP
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// 检查是否通过了代理服务器
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
// 如果包含多个IP地址取第一个
if (strpos($ip, ',') !== false) {
$ip = explode(',', $ip)[0];
}
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
// 检查是否通过了真实的IP地址
$ip = $_SERVER['HTTP_X_REAL_IP'];
} else {
// 如果没有通过代理服务器,直接获取远程地址
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$remote_addr = $_SERVER['REMOTE_ADDR'];
$forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $remote_addr;
$real_ip = $_SERVER['HTTP_X_REAL_IP'] ?? $remote_addr;
// 如果X-Forwarded-For包含多个IP地址取第一个
if (strpos($forwarded_for, ',') !== false) {
$forwarded_for = explode(',', $forwarded_for)[0];
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP地址测试接口</title>
</head>
<body>
<h4>IP地址: <?php echo htmlspecialchars($remote_addr); ?></h4>
<h4>X-Forwarded-For: <?php echo htmlspecialchars($forwarded_for); ?></h4>
<h4>X-Real-IP: <?php echo htmlspecialchars($real_ip); ?></h4>
</body>
</html>