after validating data from a registration form, redirect to another page to log in

12,549

Solution 1

Replace your code with this....

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    // validating the form to see all required fields are entered
    if (empty($_POST["userID"]))
    {
        $userErr = "User ID is required";
    }
    else
    {
        $userID = test_input($_POST["userID"]);
    }

    if (empty($_POST["password"]))
    {
        $passErr = "Password is required";
    }
    else
    {
        $password = test_input($_POST["password"]);
    }
    if (empty($_POST["pass_conf"]))
    {
        $passErrc = "Confirm your password";
    }
    else
    {
        $pass_conf = test_input($_POST["pass_conf"]);
    }

    if (empty($_POST["firstName"]))
    {
        $firstErr = "First Name is required";
    }
    else
    {
        $firstName = test_input($_POST["firstName"]);
    }

    if (empty($_POST["lastName"]))
    {
        $lastErr = "Last Name is required";
    }
    else
    {
        $lastName = test_input($_POST["lastName"]);
    }
    if (empty($_POST["middle"]))
    {
        $middle = "";
    }
    else
    {
        $middle= test_input($_POST["middle"]);
    }
    if (empty($_POST["address"]))
    {
        $addErr = "Address is required";
    }
    else
    {
        $address = test_input($_POST["address"]);
    }
    if (empty($_POST["city"]))
    {
        $cityErr = "City is required";
    }
    else
    {
        $city = test_input($_POST["city"]);
    }
    if (empty($_POST["state"]))
    {
        $stateErr = "State is required";
    }
    else
    {
        $state = test_input($_POST["state"]);
    }
    if (empty($_POST["zip"]))
    {
        $zipErr = "Zip is required";
    }
    else
    {
        $zip = test_input($_POST["zip"]);
    }
    if (empty($_POST["email"]))
    {
        $emailErr = "Email is required";
    }
    else
    {
        $email = test_input($_POST["email"]);
    }
    if (empty($_POST["phone"]))
    {
        $phoneErr = "";
    }
    else
    {
        $phone = test_input($_POST["phone"]);
    }
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        // perform sql query to insert the data
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location: login.html");
    }
}
?>

The mistake is - you are executing your redirect condition only if the REQUESTED method is NOT EQUAL to POST.

So the above code executes if the request method is POST & the password entered matches the confirm password, then redirect the user to login page. It would work now!

Solution 2

You have to exit from the code segment once the control is transferred to another page. Otherwise the following code will get executed. So use exit; after header().

header("Location:login.html");
exit;

Solution 3

try using ob_start(); at the top of the file and ob_end_flush(); at the bottom of the file.

Solution 4

just remove the else part of main if statement ans the password confirmation part put in main if statement :

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  some coce
}
//""""
else
{
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location:login.html");
    }
} //"""
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  some coce
}
//""""
else
{
    if($password != $pass_conf){
        $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
        // die($passMathErr);
    } else{
        // perform sql query to insert the data
        $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
        $result=mysql_query($sql, $connection);
        header("Location:login.html");
    }
} //"""
        // perform sql query to insert the data

remove the above """ part and add it inside main if statement

Solution 5

I could be wrong but looking at the structure of your if/else statements it looks like you would only ever send the header when the request method is NOT POST. (Ie. Looks like its in the else section.) I think its in the wrong place.

It is this else that i just think needs removing. (Although i am not sure how your validation stops the redirect with your current code)


    {$phone = test_input($_POST["phone"]);}
    }   
    else
    {
    if($password != $pass_conf){

Share:
12,549
ArsenalFC
Author by

ArsenalFC

Updated on June 27, 2022

Comments

  • ArsenalFC
    ArsenalFC almost 2 years

    i am new to php and mysql. I am trying to create a user registration form, validate the data, and then redirect to a login page, where they can enter their username and password to sign in. i have written the codes, but for some reason i'm not able to use the header() properly. Here is my code so far:

    $userErr= $passErr= $passErrc= $firstErr= $lastErr= $middle= $addErr= $cityErr=      $stateErr=$zipErr= $emailErr= $phoneErr= $passMatchErr="";
    
    $userID= $password= $pass_conf= $firstName= $lastName= $middle= $address= $city= $state= $zip= $email= $phone="";
    
    
    // Validate the form
    // use trim() function to remove unnecessary characters such as extra space, tab, newline,
    // use stripslashes(() to remove backslashes 
    // use htmlspecialchars() for security
    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    
    
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        // validating the form to see all required fields are entered
        if (empty($_POST["userID"]))
        {
            $userErr = "User ID is required";
        }
        else
        {
            $userID = test_input($_POST["userID"]);
        }
    
        if (empty($_POST["password"]))
        {
            $passErr = "Password is required";
        }
        else
        {
            $password = test_input($_POST["password"]);
        }
        if (empty($_POST["pass_conf"]))
        {
            $passErrc = "Confirm your password";
        }
        else
        {
            $pass_conf = test_input($_POST["pass_conf"]);
        }
    
        if (empty($_POST["firstName"]))
        {
            $firstErr = "First Name is required";
        }
        else
        {
            $firstName = test_input($_POST["firstName"]);
        }
    
        if (empty($_POST["lastName"]))
        {
            $lastErr = "Last Name is required";
        }
        else
        {
            $lastName = test_input($_POST["lastName"]);
        }
        if (empty($_POST["middle"]))
        {
            $middle = "";
        }
        else
        {
            $middle= test_input($_POST["middle"]);
        }
        if (empty($_POST["address"]))
        {
            $addErr = "Address is required";
        }
        else
        {
            $address = test_input($_POST["address"]);
        }
        if (empty($_POST["city"]))
        {
            $cityErr = "City is required";
        }
        else
        {
            $city = test_input($_POST["city"]);
        }
        if (empty($_POST["state"]))
        {
            $stateErr = "State is required";
        }
        else
        {
            $state = test_input($_POST["state"]);
        }
        if (empty($_POST["zip"]))
        {
            $zipErr = "Zip is required";
        }
        else
        {
            $zip = test_input($_POST["zip"]);
        }
        if (empty($_POST["email"]))
        {
            $emailErr = "Email is required";
        }
        else
        {
            $email = test_input($_POST["email"]);
        }
        if (empty($_POST["phone"]))
        {
            $phoneErr = "";
        }
        else
        {
            $phone = test_input($_POST["phone"]);
        }
    }   
    else
    {
        if($password != $pass_conf){
            $passMathErr= "Passwords do not match. Please, go back and re-enter the passwords!";
            // die($passMathErr);
        } else{
            // perform sql query to insert the data
            $sql="insert into users values('$userID', '$password', '$firstName', '$lastName', '$middle', '$address', '$city', '$state', '$zip', '$email', '$phone')";
            $result=mysql_query($sql, $connection);
            header("Location:login.html");
        }
    }
    ?>  
    
    <html lang="em">
    <head>
        <title> Registration </title>
        <style type="text/css">
            h1{
                text-align: left;
                font-weight:bold;
                font-size: 2em;
                color:#FFFF99;
                word-spacing: 0.3em;
                letter-spacing:0.1em;
                text-decoration:underline;
            }
            body{
                background-color: #421818;
            }
            .txtinput{
                margin-left:150px;
            }
            table{
                font-color:#99FF00;
            }
            .error {
                color: #FF0000;
            }
        </style>
    </head>
    <body>
        <h1> Registration Form </h1><br>
        <form name="reg" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
            <table style="color:#99FF00" border=0 cellspacing=0 cellpadding=2>
                <tr>
                    <td>User ID * <td><input type="text" name="userID"/>
                    <span class="error"><?php echo $userErr;?></span>
                </tr>
                <tr>
                    <td>Password *<td><input type="password" name="password" />
                    <span class="error"><?php echo $passErr;?></span>
                </tr>
                <tr>
                    <td>Confirm Password *<td><input type="password" name="pass_conf" />
                    <span class="error"><?php echo $passErrc;?></span>
                </tr>
                <tr>
                    <td>First Name *<td><input type="text" name="firstName" />
                    <span class="error"><?php echo $firstErr;?></span>
                </tr>
                <tr>
                    <td>Last Name *<td><input type="text" name="lastName" />
                    <span class="error"><?php echo $lastErr;?></span>
                </tr>
                <tr>
                    <td>Middle<td><input type="text" name="middle" />
                    <span class="error"><?php echo $middle;?></span>
                </tr>
                <tr>
                    <td>Address *<td><input type="text" name="address" />
                    <span class="error"><?php echo $addErr;?></span>
                </tr>
                <tr>
                    <td>City *<td><input type="text" name="city" />
                    <span class="error"><?php echo $cityErr;?></span>
                </tr>
                <tr>
                    <td>State *<td><input type="text" name="state" />
                    <span class="error"><?php echo $cityErr;?></span>
                </tr>
                <tr>
                    <td>Zip *<td><input type="text" name="zip" />
                    <span class="error"><?php echo $zipErr;?></span>
                </tr>
                <tr>
                    <td>Email *<td><input type="text" name="email"/> 
                    <span class="error"><?php echo $emailErr;?></span>
                </tr>
                <tr>
                    <td>Phone<td><input type="text" name="phone" />
                    <span class="error"><?php echo $phone;?></span>
                </tr>
            </table>
            <div class="txtinput">
                <input type="submit" name="submit" value="Register"/>   
            </div>
        </form>
    </body>
    </html>