Search code examples
phpregistration

Basic registration form, logic is wrong


I am trying to create a basic registration script. When leave all the forms blank, I get a a message saying that I have not used a valid email address, but I would like it to tell me that I have not inputted any data, and do this if any form is left blank. Logic does not seem to be correct, its probably a minor problem, but I cannot find it.

homepage.php

<html>
<head>
</head>
<body>

<?php

    require 'functions.php';

    registration_form();

?>

</body>
</html>

functions.php

function registration_form() {

echo '<form id="register" action="register.php" method="post">';
echo '<fieldset>';

echo '<legend>Register</legend>';

echo '<input type="hidden" name="submitted" id="submitted" value="1" /></br>';

echo '<label for="email">Email Address:</label></br>';
echo '<input type="text" name="email" id="email" maxlength="100" /> </br>';

echo '<label for="password">Password:</label></br>';
echo '<input type="password" name="password" id="password" maxlength="60" /></br>';

echo '<label for="confirmpassword">Confirm Password:</label></br>';
echo '<input type="password" name="confirmpassword" id="confirmpassword" maxlength="60" /></br>';

echo '<input type="submit" name="Submit" value="Submit">';

echo '</fieldset>';
echo '</form>';
}

function validate_registration($password, $confirmpassword, $email) {

    if ((!isset($email)) || (!isset($password)) || (!isset($confirmpassword))) {


    registration_form();
    echo "Please enter the required information:";

    }


    elseif ((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($password == $confirmpassword)) {

        echo "You have registered with: $email";

    }

    elseif ((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($password !== $confirmpassword)) {

        registration_form();

        echo "Your password does not match!";
    }

    else {

        registration_form();
        echo "You have not entered a valid email address!";
    }
}

register.php

require 'functions.php';

$email = $_POST['email'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];

validate_registration($password, $confirmpassword, $email);

Solution

  • You probably want to use

    if (!empty([VARIABLE]))
    

    in your validation check, as those parameter values are going to be set as zero-length strings when you POST. Thus they will will not work with !isset because the values ARE set and ARE NOT null.