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

页面1表单设置Cookie后,页面2读取时显示空白问题求助

问题原因与解决方案

核心问题分析

页面2空白的关键原因有两个:

  • 表单提交目标错误:原表单使用了无效的whenclicked属性,表单实际仍提交到当前页面(页面1),页面2从未被正确请求,自然无法读取Cookie。
  • Cookie生效时机:setcookie()设置的Cookie需要在下一次HTTP请求才会被服务器接收,你在页面1设置后立刻读取能成功是因为PHP会即时更新当前请求的$_COOKIE数组,但页面2需要新的请求才能获取。

修复步骤

1. 修正页面1的表单提交目标

把表单的action属性改为page2.php,删除无效的whenclicked属性:

<form action="page2.php" method="POST">
  <Label for="Fname">Firstname:</Label><br>
  <input type="text" id="fname"  name="fname">
  <br>
  <Label for="Lname">Lastname:</Label><br>
  <input type="text" id="lname"  name="lname">
  <br>
  <Label for="Email">Email:</Label><br>
  <input type="text" id="Email"  name="email">
  <br>
  <Label for="DOB">DOB:</Label><br>
  <input type="date" id="DOB"  name="dob">
  <br>
  <Label for="Password">Password:</Label><br>
  <input type="password" id="password"  name="password">
  <br>
  <Label for="Confirm Password">Confirm Password:</Label><br>
  <input type="text" id="cpassword"  name="cpassword">
  <input type="submit" value="Submit">
</form>

2. 调整页面1的Cookie设置逻辑

页面1只负责接收表单数据、设置Cookie,然后跳转到页面2(确保页面2能拿到新设置的Cookie)。注意header()必须在任何HTML输出之前,所以要把PHP代码移到<html>标签之前:

<?php 
if($_SERVER["REQUEST_METHOD"] == "POST") {
    if(isset($_POST["fname"], $_POST["lname"], $_POST["email"], $_POST["dob"], $_POST["password"], $_POST["cpassword"])) {
        $fname = $_POST["fname"];
        $lname = $_POST["lname"];
        $email = $_POST["email"];
        $dob = $_POST["dob"];

        // 设置Cookie,路径设为/确保全站可访问
        setcookie("fname", $fname, time() + (86400 * 30), "/");
        setcookie("lname", $lname, time() + (86400 * 30), "/");
        setcookie("email", $email, time() + (86400 * 30), "/");
        setcookie("dob", $dob, time() + (86400 * 30), "/");

        // 设置完Cookie后跳转到页面2
        header("Location: page2.php");
        exit; // 终止后续代码执行
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSE3101 -Registration</title>
</head>
<body>
<!-- 上述修正后的表单代码放在这里 -->
</body>
</html>

3. 编写页面2的Cookie读取代码

创建page2.php,专门读取并展示Cookie内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Info</title>
</head>
<body>
    <?php
    if(isset($_COOKIE["fname"], $_COOKIE["lname"], $_COOKIE["email"], $_COOKIE["dob"])) {
        $fname = $_COOKIE["fname"];
        $lname = $_COOKIE["lname"];
        $email = $_COOKIE["email"];
        $dob = $_COOKIE["dob"];
        echo "First Name: $fname <br>";
        echo "Last Name: $lname <br>";
        echo "Email: $email <br>";
        echo "Date of Birth: $dob <br>";
    } else {
        echo "未获取到用户信息,请先提交表单";
    }
    ?>
</body>
</html>

额外注意事项

  • 确保PHP的output_buffering开启,或者header()之前没有任何HTML输出(包括空格、换行),否则会报错。
  • Cookie的path参数设为/,确保在域名下的所有页面都能访问这些Cookie。
  • 测试时可以清空浏览器缓存或用隐私模式测试,避免旧Cookie干扰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 08:40:33