用php验证一个IP是否合法

$ip = $_SERVER['REMOTE_ADDR'] ?? '64.176.55.156';
if (filter_var($ip, FILTER_VALIDATE_IP)) {
    echo "有效的 IP: " . $ip;
}