Home > Blockchain >  I'm trying to set cookies from a form on page 1 and echo the information to page 2 but page 2 j
I'm trying to set cookies from a form on page 1 and echo the information to page 2 but page 2 j

Time:12-16

I'm trying to set cookies from a form on page 1 and echo the information to page 2 but page 2 just keeps showing a blank page when i try.

I set the cookies on page1 and then called them on page 2. I expected the information to show on page2 but it did not.When i run the code as a whole on page1 it prints out the information under the form. This is the code when I put it together as a whole on page one it runs normally .

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

















CodePudding user response:

Please refer to PHP manual

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

https://www.php.net/manual/en/function.setcookie.php

As per I can see you're creating the cookie after the HTML outoput.

This example is working for me:

     <?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
}
  • Related