为什么我的PHP登录脚本不起作用?

时间:2013-09-18 09:57:03

标签: php mysql mysqli

我建立了一个员工管理系统,我一直困在超级管理员需要登录的位置,以检查/改变员工的详细信息。

超级管理员只是一个可以做任何事情的人。所以我手动将超级管理员的详细信息插入到数据库中。

这是代码,页面名为superadmin.php:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>Employee Management System</title>
    <link href="../styles/style-index.css" rel="stylesheet" type="text/css" media="screen"/>
    <link href="styles/style-superAdmin.css" rel="stylesheet" type="text/css" media="screen"/>
</head>
<body>
    <?php include_once("../php_includes/pageTop-template.php") ?>
        <div id="pageContentforSuperAdmin">
            <div id="content">
                <div id="form-superAdmin" align="center">
                    <h3>Please Login Super Admin</h3>           
                    <form id="superAdminForm" method="post" action="superadmin.php">
                        <input type="email" name="email" required class="txtInput" placeholder="Email..." autocomplete="off"/>
                        <br />
                        <input type="password" name="password" required class="txtInput" placeholder="Password..."/>
                        <br />
                        <input type="submit" name="submit" value="Enter" id="submit" />
                    </form>
                </div>
            </div>
        </div>
    <?php include_once("../php_includes/pageBottom-template.php") ?>
</body>
</html>

<?php
    include_once("../php_includes/db-connect.php");

    if (isset($_POST["submit"])) {
        $email = mysqli_real_escape_string($con, $_POST["email"]);
        $password = mysqli_real_escape_string($con, $_POST["password"]);

        $hash = password_hash($password, PASSWORD_DEFAULT);
        //echo $password;
        $hash_ver = password_verify($password, $hash);

        $sql = "SELECT * FROM employee WHERE email='".$email."' AND password='".$hash."'";
        $query = mysqli_query($con, $sql);
        $rows = mysqli_fetch_array($query);

        if ($rows["email"] == $email && $rows["password"] == $hash) {
            header("Location: admin-index.php");
        } else {
            echo 'Incorrect Credentials';
        }
    }

    mysqli_close($con);

?>

现在的问题是,我只是将输出作为不正确的凭据而我不知道原因是什么呢?对于密码,我使用了新引入的password_hash进行哈希处理。我已将哈希代码直接复制并粘贴到数据库表中。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

除了其他任何内容之外,我建议您尝试用此替换if

if (mysqli_num_rows($query) > 0) {
    header("Location: admin-index.php");
} else {
    echo 'Incorrect Credentials';
}

因为您已经从用户名和密码匹配的数据库中进行选择。

答案 1 :(得分:0)

if ($rows["email"] == $email && $rows["password"] == $hash) {

应更改

以检查是否找到$query的记录。

像:

if(mysql_fetch_array($query) !== false) {

相关问题