0

Full error: Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/431/18637431/public_html/mysql.php:1) in /storage/ssd2/431/18637431/public_html/login.php on line 26 I builded an login system with php and I want to save the user that is logged in, in a php cookie called name but i get this error . why?

enter code here
        <?php
    $correct = false;
    if(isset($_POST["submit"])){
      require("mysql.php");
      $stmt = $mysql->prepare("SELECT * FROM accounts WHERE USERNAME = :user"); //Username überprüfen
      $stmt->bindParam(":user", $_POST["username"]);
      $stmt->execute();
      $count = $stmt->rowCount();
      if($count == 1){
        //Username ist frei
        $row = $stmt->fetch();
        if(password_verify($_POST["pw"], $row["PASSWORD"])){
            $correct = true;
            $userna = $row["USERNAME"];
         // session_start();
         // $_SESSION["username"] = $row["USERNAME"];
          // header("Location: index.html");
          
        } else {
          echo "Der Login ist fehlgeschlagen";
        }
      } else {
        echo "Der Login ist fehlgeschlagen";
      }
      if($correct ==true){
         setcookie("name","$userna");  
         
              echo("Anmeldung war erfolgreich. ");
               //echo("Hallo " + $userna);
                         



//setcookie("name","$userna");
           
        }
    }
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Login</title>
  </head>
  <body>
    <h1>Anmelden</h1>
    <form action="login.php" method="post">
      <input type="text" name="username" placeholder="Username" required><br>
      <input type="password" name="pw" placeholder="Passwort" required><br>
      <button type="submit" name="submit">Einloggen</button>
    </form>
    <br>
    <a href="register.php">Noch keinen Account?</a>
    <script>
   
    var name = "<?=  $userna  ?>";
    document.cookie = "user=" + name;
   // mycookie = document.cookie;
    alert("Hallo "+ name  + " du wurdest erfolgreich eingeloggt"  );


        
    </script>
  </body>
  • 2
    Does this answer your question? [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Andrea Olivato Apr 10 '22 at 10:52
  • 1
    All header-related functions (`header`, `session_start`, and your own `setcookie`) must be called before any other output in the page. The error tells you you have some output before the function. It could also be a simple white space before the ` – Andrea Olivato Apr 10 '22 at 10:53
  • 3
    Please do some basic research when you get an error. Start by googling the error message. This error has already been explained over and over many times before, both here on stack overflow and on thousands of other sites. If you _have_ researched, then you need to explain (detailed) what you've already tried and what didn't work so we don't spend time suggesting what you've already tried. – M. Eriksson Apr 10 '22 at 11:00
  • _"output started at /storage/ssd2/431/18637431/public_html/mysql.php:1"_ so it seems to be your `mysql.php` file which you include that contains some output (could be an echo, white space/line break before php open tags or after close tags or some error/warning that gets outputted). – M. Eriksson Apr 10 '22 at 11:07

0 Answers0