PHP登录表单不起作用,我该怎么办才能修复它?

时间:2018-05-20 12:38:10

标签: php mysql database login

我有以下两个php脚本:
config.php中:

<?php
define('DB_SERVER', 'localhost:3306');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'mind3');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

和index.php,其中包含登录脚本和表单:

    <?php
require_once 'config.php';
$username=$password='';
$username_err=$password_err='';
if($_SERVER["REQUEST_METHOD"] == "POST"){

    if(empty(trim($_POST["username"]))){
    $username_err = 'Please enter an username!';
    } else{
    $password = trim($_POST['username']);  
    }

    if(empty(trim($_POST["password"]))){
    $pasword_err = "Please enter a password!";
    } else{
    $password = trim($_POST['password']);
    }

    if(empty($username_err) && empty($password_err)){
    $sql = "SELECT username,password FROM members WHERE username = ?";

    if($stmt = mysqli_prepare($link, $sql)){
        mysqli_stmt_bind_param($stmt, "s", $param_username);
        $param_username = $username;
        if(mysqli_stmt_execute($stmt)){
        mysqli_stmt_store_result($stmt);
        if(mysqli_stmt_num_rows($stmt) == 1){
            if(password_verify($password, $hashed_password)){
            session_start();
            $_SESSION['username'] = $username;
            header("location: welcome.php");

            } else{
            $username_err = 'Username/password is wrong!';
        }
        } else{
        echo "Oops! Something went wrong. Please try again later.";
        }
    }
    mysqli_stmt_close($stmt);
    } 
    mysqli_close($link);
} 
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div class="wrapper">
    <h2>Log in</h2>
    <form action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group <?php echo(!empty($username_err)) ? 'has-error' : ''; ?>">
            <input type="text" name="username" class="form-control" placeholder="Username">
            <span class="help-block"><?php echo $username_err; ?></span>
        </div>
        <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <input type="password" name="password" class="form-control">
            <span class="help-block"><?php echo $password_err; ?></span>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Login">
        </div>
    </form>
    </div>
</body>
</html>

问题是每当我输入我在数据库中的凭据时,它都不会将我重定向到welcome.php,它也不会告诉我密码或用户名是错误的。但是当我不知道时#39;输入用户名或密码我得到&#34;请输入用户名&#34;或者&#34;请输入密码&#34;。

我不明白出了什么问题。

3 个答案:

答案 0 :(得分:1)

您将用户名设置为$password变量,而不是$username变量。

if(empty(trim($_POST["username"]))){
  $username_err = 'Please enter an username!';
} else{
  $password = trim($_POST['username']);  
}

只需将$password =替换为$username = ...

答案 1 :(得分:0)

您的代码在第10行的第二个代码段中出错。 以下是修补程序的代码段,请查看代码注释以获取更多信息。

    <?php
    require_once 'config.php';
    $username=$password='';
    $username_err=$password_err='';
    if($_SERVER["REQUEST_METHOD"] == "POST"){

    if(empty(trim($_POST["username"]))){
    $username_err = 'Please enter an username!';
    } else{
    //you got error here (was $password)
    $username = trim($_POST['username']);  
    }

    if(empty(trim($_POST["password"]))){
    $pasword_err = "Please enter a password!";
    } else{
    $password = trim($_POST['password']);
    }

    if(empty($username_err) && empty($password_err)){
    $sql = "SELECT username,password FROM members WHERE username = ?";

    if($stmt = mysqli_prepare($link, $sql)){
        mysqli_stmt_bind_param($stmt, "s", $param_username);
        $param_username = $username;
        if(mysqli_stmt_execute($stmt)){
        mysqli_stmt_store_result($stmt);
        if(mysqli_stmt_num_rows($stmt) == 1){
            if(password_verify($password, $hashed_password)){
            session_start();
            $_SESSION['username'] = $username;
            header("location: welcome.php");

            } else{
            $username_err = 'Username/password is wrong!';
        }
        } else{
        echo "Oops! Something went wrong. Please try again later.";
        }
    }
    mysqli_stmt_close($stmt);
    } 
    mysqli_close($link);
} 
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div class="wrapper">
    <h2>Log in</h2>
    <form action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group <?php echo(!empty($username_err)) ? 'has-error' : ''; ?>">
            <input type="text" name="username" class="form-control" placeholder="Username">
            <span class="help-block"><?php echo $username_err; ?></span>
        </div>
        <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <input type="password" name="password" class="form-control">
            <span class="help-block"><?php echo $password_err; ?></span>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Login">
        </div>
    </form>
    </div>
</body>
</html>

答案 2 :(得分:0)

您没有正确设置$ username的值。

更改:

$password = trim($_POST['username']); 

$username = trim($_POST['username']);

$ hashed_pa​​ssword必须有一个值,因为在password_verify()中需要它。使用mysqli的bind_result和fetch方法来获取所需的值。

<强>后:

if(mysqli_stmt_num_rows($stmt) == 1){

添加

$hashed_password = "";
$username2 = "";

mysqli_stmt_bind_result($stmt, $username2, $hashed_password);
mysqli_stmt_fetch($stmt);

添加了exit();在标题(..)重定向之后,重定向调用后不会执行其他代码。

更新代码:

<?php
require_once 'config.php';
$username = $password = '';
$username_err = $password_err = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty(trim($_POST["username"]))){
        $username_err = 'Please enter an username!';
    }
    else {
        $username = trim($_POST['username']);
    }

    if (empty(trim($_POST["password"]))){
        $pasword_err = "Please enter a password!";
    }
    else {
        $password = trim($_POST['password']);
    }

    if (empty($username_err) && empty($password_err)) {
        $sql = "SELECT username,password FROM members WHERE username = ?";

        if ($stmt = mysqli_prepare($link, $sql)) {
            mysqli_stmt_bind_param($stmt, "s", $param_username);
            $param_username = $username;
            if (mysqli_stmt_execute($stmt)){
                mysqli_stmt_store_result($stmt);
                if (mysqli_stmt_num_rows($stmt) == 1){
                    $hashed_password = "";
                    $username2 = "";

                    mysqli_stmt_bind_result($stmt, $username2, $hashed_password);
                    mysqli_stmt_fetch($stmt);
                    if (password_verify($password, $hashed_password)){

                        $_SESSION['username'] = $username;
                        mysqli_stmt_close($stmt);
                        mysqli_close($link);
                        header("location: welcome.php"); exit();

                    }
                    else{
                        $username_err = 'Username/password is wrong!';
                    }
                }
                else {
                    echo "Oops! Something went wrong. Please try again later.";
                }
            }
            mysqli_stmt_close($stmt);
        }
        mysqli_close($link);
    }
}
?>