php 我尝试从第1页上的表单设置Cookie并将信息回显到第2页,但当我尝试时,第2页一直显示空白页

2uluyalo  于 2022-12-17  发布在  PHP
关注(0)|答案(1)|浏览(133)

我试图从第1页的表单设置cookie,并将信息回显到第2页,但当我尝试时,第2页一直显示空白页。
我在page1上设置了cookie,然后在page2上调用了它们。我希望信息显示在page2上,但它没有。当我在page1上作为一个整体运行代码时,它会打印出表单下的信息。这是我在page1上作为一个整体将其放在一起时的代码,它正常运行。

<!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>

    <form action=""   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" whenclicked= "page2.php">
    </form>

   <?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"];
            $password = $_POST["password"];
            $cpassword = $_POST["cpassword"];

            setcookie("fname", $fname, time() + (86400 * 30), "/");
            setcookie("lname", $lname, time() + (86400 * 30), "/");
            setcookie("email", $email, time() + (86400 * 30), "/");
            setcookie("dob", $dob, time() + (86400 * 30), "/");
         

                
                               

        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 "Cookies not set";
        }
                

            }


        }

    

    ?>

            
            

              

            

            </body>
</html>
dced5bon

dced5bon1#

请参考PHP手册
setcookie()定义了一个与HTTP头沿着发送的cookie。与其他头一样,cookie必须在脚本输出之前发送(这是一个协议限制)。这要求您在任何输出之前调用此函数,包括和标记以及任何空白。
https://www.php.net/manual/en/function.setcookie.php
根据我可以看到你创建的HTML输出后的cookie。
这个例子对我很有效:

<?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"];
      $password = $_POST["password"];
      $cpassword = $_POST["cpassword"];

      setcookie("fname", $fname, time() + (86400 * 30), "/");
      setcookie("lname", $lname, time() + (86400 * 30), "/");
      setcookie("email", $email, time() + (86400 * 30), "/");
      setcookie("dob", $dob, time() + (86400 * 30), "/");

      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 "Cookies not set";
      }
    }
  

    } else {
    ?>
    
    <!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>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>"   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" whenclicked= "page2.php">
    </form>        
  </body>
</html>
<?php
}

相关问题