mardi 1 mai 2018

Why and where is my header already being sent? [duplicate]

This question already has an answer here:

When checking if the username ($resultcheck) has been taken, it should send out a header if the result is over 0 (1). The problem is that I'm constantly getting the error "header already sent". I cant figure out where I have any blank spaces and I've started the session at the beginning. Why is my header not being sent? Right now, If they create a user with a username that exists, it simply refreshes the page and it doesnt create the user. THe same would be if I checked for the same email. I've removed some unecessary html code from the snippet down below, I removed things such as style and links to different fonts. Would having a header inside if and else statements normally cause trouble like mine or have I done an error in the code?

<?php session_start();
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>IT-Prosjekt</title>

    </head>
    <script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
    <?php
    include'dbh.php';
    global $sql;
    global $sql2;
    if (isset($_SESSION['u_id'])) {
      echo '       
                <center>
                    <ul class="animated bounceInDown nav-login">
                        <li>
                                            <h1 class="nav-login-info">Velkommen,  '.$_SESSION['u_uid'].', du er nå logget inn! </h1> </li> 
                        <li style="width:6%"></li>
                        <li>
                                <ul class="loginarea" style="width:100%;"><li><form action="logout.php" method="POST">
                                <button type="submit" name="submit" style="background-color:whitesmoke;;color:black;border-bottom: solid 2px black;">Logg ut!</button>
                            </form></ul>                        </li>
                        </ul>
                </center>
    ';
    } else {
      echo '       
                <center>
                    <ul class="animated bounceInDown nav-login">
                        <li>
                                            <h1 class="nav-login-info">Her kan du logge inn!</h1>                        </li>
                        <li style="width:6%"></li>
                        <li>
                                <form action="./login.php" method="POST">
                                <input type="text" name="uid" placeholder="Brukernavn/Mail" required style=" border-radius: 25px;">
                                <input type="password" name="pwd" placeholder="Passord" required style=" border-radius: 25px;">
                                <button type="submit" name="submit"><img src="https://d30y9cdsu7xlg0.cloudfront.net/png/1560-200.png" style="position:absolute;margin-top:2px;margin-left:-20px; width:20px;height:17px;">Logg inn</button>
                            </form>
                            <a class="signup" href="signup.php">Registrer deg</a>                        </li>
                        </ul>
                </center>

    </div>';
    }
    ?>
    <?php

    if (isset($_SESSION['u_id'])) {
        echo '<br><br><br><br><br><h1>Du er allerede registrert og logget inn! Logg ut for å registrere en ny bruker!</h1>';
    } else {

        echo '
                    <div class="contentbox">
                        <center>

            <h1></h1>
                            <br>
                                <img src="https://cdn1.iconfinder.com/data/icons/social-messaging-productivity-4/128/add-user-3-512.png" alt="newuser" width="150px" height="150px">
                    <br>
                <p style="font-family: "Quicksand", sans-serif;">Registrer brukeren din her!</p>

                    <form class="signup-form" action="" method="POST">
                        <input type="text" name="first" placeholder=" Fornavn" required style=" border-radius: 25px;">
                        <input type="text" name="last" placeholder=" Etternavn" required style=" border-radius: 25px;">
                        <input type="email" name="email" placeholder=" E-mail" required style=" border-radius: 25px;">
                        <input type="text" name="uid" placeholder=" Brukernavn" required style=" border-radius: 25px;">
                        <input type="password" name="pwd" placeholder=" Passord" required style=" border-radius: 25px;">
                        <button type="submit" style="border-radius:25px;font-weight:bold;"name="submit">Lag en bruker</button>
                    </form>
                </center>
            </div>

    '; 
    }
    ?>
    <?php if (isset($_POST['submit'])) {
        $first = $_POST['first'];
        $last = $_POST['last'];
        $email = $_POST['email'];
        $uid = $_POST['uid'];
        $pwd = $_POST['pwd'];

        // Error handlers
        // Check for empty fields
       if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
            header("Location: ./signup.php?signup=empty");
            exit();    
        } else {
            //Check if input characters are valid
        if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
            header("Location: ./signup.php?signup=invalid");
            exit();   
            } else {
            // Check if email is valid
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            header("Location: ./signup.php?signup=email");
            exit();  
            } else {
                $sql = "SELECT * FROM users WHERE user_uid='$uid'";
                $result = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);

            if ($resultCheck > 0) {
                header("Location: ./signup.php?signup=usertaken");
                exit();  
            } else {
                // Hashing the password
                $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
                // Insert the user into the database
                $sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";
            mysqli_query($conn, $sql);
                header("Location: ./signup.php?signup=success");
                exit();  
            }
        }
        }
    }

    } else {
        header("Location: ./signup.php");
        exit();
    }
    print '
    </div>
    </body>
    </html>
    ';

Aucun commentaire:

Enregistrer un commentaire