mercredi 30 novembre 2016

isset and !empty always returns true for login field

I'm trying to create a php file (login.php) that can check the login field and validate the username and password. At the moment, it's just checking username using regex. Below is my code. I can't trigger the second else clause, it seems that isset and !empty are always true, even when I can see that they're visually empty, like immediately after refreshing the page.

<?php 
if (isset($_POST['login']) && !empty($_POST['login'])) {
    if (preg_match('/^[a-z0-9]{6,15}$/i', $_POST['username'])) {
        echo 'set';          
    }
    else {
        echo 'wrong';
    }
} else {
    echo 'required field';
}   
?>

<div id="login"> <!-- Login field with link to registration -->
    <form method="POST" action="login.php">
    <Legend>Login</Legend>
    Username <input type="text" name="username"/>
    Password <input type="password" name="password"/>
    <input type="submit" name="login">
        <div id="register">
            <a href="registration.html">Not a member? Click here to register!</a>
        </div>
    </form>     
</div>

Aucun commentaire:

Enregistrer un commentaire