Redirecting from registration page to login page on HTML

I'm new to PHP and html coding. I'm making a simple login and registration form backed up by my database.

I want my registration page to redirect to my login page once a user registration is successful.

User hits Register-> if successful, "successfully registered" will be shown on the same page -> redirects to Login page.

But in my php coding for the registration page, I included "if" statements to show error messages for every field (for the case if user didnt fill it).

So, even when I include the header('location: login.php') command, it does not redirect to my login page. Instead, I enter a new page and the error message "Full name should not be empty" pops up. I dont know what I'm doing wrong. Please help.

Here's my code for the registration page. Just to clarify, my registration page has 8 fields to be keyed by user, in which 6 of them are 'required'. The required fields are from Full name to Confirm Password.

On the sidenote, I am trying to include error messages to pop up for every field. Eg; "Username should not be empty" and "passwords do not match". How can I implement that?

<?php
header('location:login.php');
error_reporting (E_ALL ^ E_NOTICE);

$fullname = filter_input(INPUT_POST, 'fullname');
$username = filter_input(INPUT_POST, 'username');
$email = filter_input(INPUT_POST, 'email');
$contactnum = filter_input(INPUT_POST, 'contactnum');
$password = filter_input(INPUT_POST, 'password');
$conpass = filter_input(INPUT_POST, 'conpass');
$expertise = filter_input(INPUT_POST, 'expertise');
$description= filter_input(INPUT_POST, 'description');

if(!empty($fullname)){
    if(!empty($username)){
        if(!empty($email)){
            if(!empty($contactnum)){                                                            
                if(!empty($password)){
                    $host = "localhost";
                    $dbusername = "root";
                    $dbpassword = "";
                    $dbname = "searchexperts";
                    
                    $conn= new mysqli ($host,$dbusername,$dbpassword,$dbname);
                    if(mysqli_connect_error()){
                        die('Connection Error ('.mysqli_errno().')'.mysqli_connect_error());
                    } else {
                        $sql = "INSERT INTO newregister
                                    (Fullname,Username,Email,Contactnumber,
                                    Password,Expertise,Description) 
                            VALUE('$fullname','$username','$email',
                                '$contactnum','$password',
                                '$expertise','$description')";
                        if($conn->query($sql)){
                            echo " Successfully Registered!";
                        }else{
                            echo "Error :".$sql."<br>".$conn->error;
                        }
                        $conn->close;
                    }
                }else{
                    echo "Password should not be empty";
                }                                                   
            }else{
                echo "Contact number should not be empty";
            }
        }else{
            echo "Email should not be empty";
        }
    }else{
        echo "Username should not be empty";
    }
}else{
    echo "Full name should not be empty";
}
?>


Read more here: https://stackoverflow.com/questions/64411758/redirecting-from-registration-page-to-login-page-on-html

Content Attribution

This content was originally published by Bernard Matt at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: