登录表单验证消息

时间:2017-09-25 13:19:25

标签: php html forms validation login

每当我尝试使用不正确的信息登录时,我都没有收到错误消息,当我尝试使用不正确的信息登录时,它只会重置我的表单。我想我可能在某处有一个冲突的代码。我的代码有问题吗?或者,如果可能的话还有其他方法可以根据我的代码提供验证吗?

一切正常。我只需要验证。

我的PHP:

<?php

session_start();
ob_start();

//Include the database connection file
include "database_connection.php";

//Check to see if the submit button has been clicked to process data
if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
{
    //Variables Assignment
    $username = trim(strip_tags($_POST['username']));
    $user_password = trim(strip_tags($_POST['passwd']));


    $validate_user_information = mysql_query("select * from `signup_and_login_users_table` where `username` = '".mysql_real_escape_string($username)."' and `password` = '".mysql_real_escape_string($user_password)."'");

    //Validate against empty fields
    if($username == "" || $user_password == "")
    {
        $error = '<br><div class="info">Sorry, all fields are required to log into your account. Thanks.</div><br>';
    }
    elseif(mysql_num_rows($validate_user_information) == 1) //Check if the information of the user are valid or not
    {
        //The submitted info of the user are valid therefore, grant the user access to the system by creating a valid session for this user and redirect this user to the welcome page
        $get_user_information = mysql_fetch_array($validate_user_information);
        $_SESSION["VALID_USER_ID"] = $username;
        $_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["fullname"]);
        header("location: home.php");
    }
    else
    {
        //The submitted info the user are invalid therefore, display an error message on the screen to the user
        $error = '<br><div class="info">Sorry, you have provided incorrect information. Please enter correct user information to proceed. Thanks.</div><br>';
    }

}
?>

我的表格:

<div class="login">
    <font color="black" size="5"><p>Employee Login</p></font>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="text" name="username" placeholder="Username" required="required" />
        <input type="password" name="passwd" placeholder="Password" required="required" />
        <input type="hidden" name="submitted" id="submitted" value="yes">
        <button type="submit" name="submit" class="btn btn-primary btn-block btn-large">Login</button>
        <p></p>
        <a href="index.php"><img src="img/homebutton.png" height="35px" width="35px">
    </form>
</div>

1 个答案:

答案 0 :(得分:1)

首先使用mysqli函数而不是mysql,因为它们现已弃用。

其次,您没有收到错误消息的原因是您没有打印错误消息。在定义echo $error;变量

后,您应添加error