You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何XAMPP本地环境下PHP文件出现err_too_many_redirects错误?

解决XAMPP中PHP的err_too_many_redirects错误

问题根源

核心问题是index.php中通过include 'login.php'引入了完整的登录验证文件,而login.php的逻辑是无论session状态如何,都会执行header('Location: ...')强制跳转:

  • 当用户通过login.php验证后跳转到index.php,index.php立刻包含login.php;
  • login.php检测到$_SESSION['ip_allowed']已存在,又会跳回index.php;
  • 如此反复形成无限跳转循环,触发浏览器的err_too_many_redirects错误。

解决方案

移除index.php中的include 'login.php'语句,index.php仅需独立完成登录状态校验即可,无需重复引入带跳转逻辑的login.php。

修改后的index.php代码

<?php
session_start();

// 直接校验登录状态,不再引入login.php
if (!isset($_SESSION['ip_allowed'])) {
    header('Location: access_denied.html');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome!</title>
</head>
<body>
    <h1>
        Your IP Is Allowed!✅
    </h1>
    <a href="login.php">
        Back To Login
    </a>
</body>
</html>

可选优化(复用验证逻辑)

如果需要在多个页面复用IP校验逻辑,建议将login.php中的验证代码封装成独立函数,放到单独的文件(比如auth.php)中,再在需要的页面调用:

  1. 新建auth.php:
<?php
function checkIpAccess() {
    // 原login.php中的MySQL校验逻辑
    // <The php codes that checks from mysql...>
    
    global $count;
    if ($count === 1) {
        $_SESSION['ip_allowed'] = true;
        return true;
    }
    return false;
}
?>
  1. 修改login.php:
<?php
session_start();
include 'auth.php';

if (!isset($_SESSION['ip_allowed'])) {
    if (checkIpAccess()) {
        header('Location: index.php');
        exit;
    } else {
        header('Location: access_denied.html');
        exit;
    }
} else {
    header('Location: index.php');
    exit;
}
?>
  1. index.php保持之前修改后的版本即可。

内容的提问来源于stack exchange,提问作者ail torpak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 23:33:14