0

I am getting a "Cannot modify header information - headers already sent". I know this has been asked a lot but I still cannot get it working after going through other answers.

Here is my code.

<!DOCTYPE html>
<html>
    <head>
      <link rel="stylesheet" type="text/css" href="style/index.css">
      <script type="text/javascript">
          function loadRegister() {
              document.getElementById('log_container').style.display = "none";
              document.getElementById('reg_container').style.display = "block";
          }
          function loadLogin() {
              document.getElementById('reg_container').style.display = "none";
              document.getElementById('log_container').style.display = "block";
          }
     </script>
    </head>

    <body>
        <div id = "container_div">
          <div id = "logo_container">
            <img id = "polygon_logo" src="images/logo.png"/>
          </div>
          <h1>POLYGON</h1>
          <div id = "log_container" class="login container">
            <form id="register_form" method="post" action="index.php">
              <input type="text" name="username" class="input" placeholder="Your Username!"><br>
              <input type="password" name="pass" class="input" placeholder="Your Password!"><br>
              <input type="submit" name="submit_login" value="Login" class="action-btn">
              <input type="button" name="go-to-register" value="Don't have an account? Register" class="text-btn" onclick="loadRegister();">
            </form>
          </div>
          <div id = "reg_container" class="register container">
            <form id="register_form" method="post" action="index.php">
              <input type="text" name="username" class="input" placeholder="Your Username!" required><br>
              <input type="text" name="useremail" class="input" placeholder="Your Email!" required><br>
              <input type="password" name="pass" class="input" placeholder="Your Password!" required><br>
              <select class="country" name="country_code">
                    <option value="AF">Afghanistan</option>
                    <option value="AX">Åland Islands</option>
                    <option value="AL">Albania</option>
                    <option value="DZ">Algeria</option>
                    <option value="ZW">Zimbabwe</option>
                </select>
              <input type="submit" name="submit_register" value="Register" class="action-btn">
              <input type="button" name="go-to-login" value="Already have an account? Login" class="text-btn" onclick="loadLogin();">
            </form>
          </div>
        </div>

        <?php
          include 'connect.php';
          if(isset($_POST['submit_register'])) {
            $name=$_POST['username'];
            $mail=$_POST['useremail'];
            $pass = $_POST['pass'];
            $country = $_POST['country_code'];
            $hash = hash('sha512',$pass);
            $sql = "INSERT INTO user_details (user_name, user_email, user_password, country) VALUES ('$name','$mail','$hash', '$country')";
            if ($conn->query($sql) === TRUE) {
              //mail($mail,'Welcome','Thank you for signing up with Polygon','From: dfarrelly96@gmail.com');
              header('Location: home.php');
              echo "New record created successfully";
            } else {
              echo "Error: " . $sql . "<br>" . $conn->error;
            }
          }

          if(isset($_POST['submit_login'])) {
            $name=$_POST['username'];
            $pass = $_POST['pass'];
            $hash = hash('sha512',$pass);
            $sql = "SELECT user_name,user_password FROM user_details WHERE user_name='$name' AND user_password='$hash'";
            $result = $conn->query($sql);
            if($result->num_rows > 0) {
              // Set session variables
              $_SESSION["name"] = $name;
              $_SESSION["mail"] = $mail;
              $sql = "SELECT user_id FROM user_details WHERE user_name ='$name'";
              $result = $conn->query($sql);
              if($result->num_rows > 0) {
                  $row = mysqli_fetch_array($result);
                  $_SESSION["user_id"] = $row["user_id"];
                  header('Location: home.php');
              }
              else {
                echo "Unknown Error!";
              }
            }
            else {
              echo "Incorrect username or password. Please try again!";
            }
          }
        ?>
    </body>
</html>

The warning says output started at C:\xampp\htdocs\Polygon\index.php:293 which is the first "

edit: forgot to mention I am getting this for both forms, if that makes a difference?

daveyjones
  • 123
  • 3
  • 10
  • 3
    You have `header()` calls in the middle of your html. `header()` calls have to be made __before anything__ gets outputted – Patrick Evans Dec 17 '16 at 00:09
  • 1
    And all the HTML you have before your first ` – jmoerdyk Dec 17 '16 at 00:13
  • @PatrickEvans so should I just place my php at the start? in ? – daveyjones Dec 17 '16 at 00:14
  • 1
    @DavidFarrelly - a `header` call MUST go **before anything**, including before the `DOCTYPE` or `` tags. For this reason, typically you'll put all PHP "logic" before the html output. In your case, ALL PHP should go before any markup. – random_user_name Dec 17 '16 at 00:17
  • http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – D. Wilson Dec 17 '16 at 00:19
  • Ah I see, thank's guys, I have always put my PHP in the tag, that explains why I have so many problems. – daveyjones Dec 17 '16 at 00:20
  • it is only necessary to do header and session related stuff before the output. Obviously not every PHP script uses header redirects and/or sessions, so .. Also, you may have the ability to change the server settings and enable "output buffering" , then Apache sorts it out for you. – Duane Lortie Dec 17 '16 at 00:42

0 Answers0