samedi 19 mai 2018

The header function is not working

I created a signup form and once a user has successfully signed up, it should redirect to home.php. But instead, it's being redirected to index.php. Everything else works (please see the code below). I could see that a user was added to my database, and that the status of the profile picture is 1.

<?php

if (isset($_POST['submitSignup'])) {

include_once 'dbh.inc.php';

$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['passwordS'];

if (empty($first) || empty($last) || empty($email) || empty($username) || empty($password)) {
    header("Location: ../signup.php?signup=empty"); exit();}
else{
    if (!preg_match("/^[A-Za-z\s'-]{2,50}$/", $first) || !preg_match("/^[A-Za-z\s'-]{2,50}$/", $last)) {
        header("Location: ../signup.php?signup=flnameinvalid&email=$email&username=$username"); exit();}
    else{
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            header("Location: ../signup.php?signup=emailinvalid&first=$first&last=$last&username=$username"); exit();}
        else{
            $mysql = "SELECT * FROM users WHERE user_username='$username';";
            $result = mysqli_query($conn, $mysql);
            $resultCheck = mysqli_num_rows($result);

            if ($resultCheck > 0) {
                header("Location: ../signup.php?signup=unametaken&first=$first&last=$last&email=$email"); exit();}
            else{
                if (strlen($password) < 8) {
                    header("Location: ../signup.php?signup=strlen&first=$first&last=$last&email=$email&username=$username"); exit();}
                else{
                    if (!preg_match("#[0-9]+#", $password)) {
                        header("Location: ../signup.php?signup=num&first=$first&last=$last&email=$email&username=$username"); exit();}
                    else{
                        if (!preg_match("#[A-Z]+#", $password)) {
                            header("Location: ../signup.php?signup=cap&first=$first&last=$last&email=$email&username=$username"); exit();}
                        else{
                            $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                            $sql = "INSERT INTO users (user_first, user_last, user_email, user_username, user_password) 
                                    VALUES (?, ?, ?, ?, ?);";
                            $stmt = mysqli_stmt_init($conn);

                            if (!mysqli_stmt_prepare ($stmt, $sql)) {
                                echo "SQL error!";}
                            else{
                                mysqli_stmt_bind_param ($stmt, "sssss", $first, $last, $email, $username, $hashedPwd);
                                mysqli_stmt_execute ($stmt);



                                $sql3 = "SELECT * FROM users WHERE user_username='$username' AND user_first='$first';";
                                $result = mysqli_query($conn, $sql3);

                                if (mysqli_num_rows($result) > 0) {
                                    while ($row = mysqli_fetch_assoc($result)) {
                                        $userid = $row['id'];
                                        $sql4 = "INSERT INTO profileimg (userid, status) VALUES ($userid, 1);";
                                        mysqli_query($conn, $sql4);
                                    }
                                }
                                else{ echo "There are no users!";}
                            }
                            header("Location: ../home.php?signup=success"); exit();
                        }
                    }
                }
            }
        }
    }
}

}

else{header("Location: ..signup.php?signup=error");}

The header function doesn't work even if I placed it after the mysqli_execute($stmt). I tried creating a user-defined function instead, but it is still not working. It was working last night when I haven't added the last if and else statement yet, the one with $sql3. Please help me find out where I went wrong.

Aucun commentaire:

Enregistrer un commentaire