doushan1863 2016-01-14 21:01
浏览 65
已采纳

PHP PDO登录未找到用户名和密码

So i followed a tutorial that shows how to login,

But i made a username and password in my phpmyadmin, But everytime when i try to login it says: Username or Password not found this is the code;

    <!--Begin webshop WOOOH-->

<?php


session_start();

    //DB configuration Constants
    include("class.php");
    //PDO Database Connection
    try {
        $databaseConnection = new PDO('mysql:host='._HOST_NAME_.';dbname='._DATABASE_NAME_, _USER_NAME_, _DB_PASSWORD);
        $databaseConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

    if(isset($_POST['submit'])){
        $errMsg = '';
        //username and password sent from Form
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);

        if($username == '')
            $errMsg .= 'You must enter your Username<br>';

        if($password == '')
            $errMsg .= 'You must enter your Password<br>';


        if($errMsg == ''){
            $records = $databaseConnection->prepare('SELECT id,username,password FROM  tbl_users WHERE username = :username');
            $records->bindParam(':username', $username);
            $records->execute();
            $results = $records->fetch(PDO::FETCH_ASSOC);
            if(count($results) > 0 && password_verify($password, $results['password'])){
                $_SESSION['username'] = $results['username'];
                header('location:dashboard.php');
                exit;
            }else{
                $errMsg .= 'Username and Password are not found<br>';
            }
        }
    }

?>

<html>
<head>

    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="css/reset.css">

    <title>Webshop 2016</title>

</head>
<body>

    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="#">Info</a></li>
                <li><a href="#">Login</a></li>
                <li><a href="#">Webshop</a></li>
                <li><a href="#">Home</a></li>
            </ul>
        </div>
        <!--Main content!-->
        <div id="content">

<div align="center">
        <div style="width:300px; border: solid 1px #006D9C; " align="left">
            <?php
                if(isset($errMsg)){
                    echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$errMsg.'</div>';
                }
            ?>
            <div style="background-color:#006D9C; color:#FFFFFF; padding:3px;"><b>Login</b></div>
            <div style="margin:30px">
                <form action="" method="post">
                    <label>Username  :</label><input type="text" name="username" class="box"/><br /><br />
                    <label>Password  :</label><input type="password" name="password" class="box" /><br/><br />
                    <input type="submit" name='submit' value="Submit" class='submit'/><br />
                </form>
            </div>
        </div>
    </div>

        </div>
        <div id="footer">
            Footer
        </div>
    </div>

</body>
</html>

This is the class.php (the connection file)

    <?php
define('_HOST_NAME_', 'localhost');
define('_USER_NAME_', 'root');
define('_DB_PASSWORD', '####');
define('_DATABASE_NAME_', 'ws_webshop');

//PDO Database Connection
try {
 $databaseConnection = new PDO('mysql:host='._HOST_NAME_.';dbname='._DATABASE_NAME_, _USER_NAME_, _DB_PASSWORD);
 $databaseConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
 echo 'ERROR: ' . $e->getMessage();
}

?>

Does anyone see the problem?

the user and pass is demo / demo but he says theres no Username or password found...

  • 写回答

1条回答 默认 最新

  • douke6424 2016-01-14 21:06
    关注

    From the docs:

    Returns TRUE if the password and hash match, or FALSE otherwise.

    If you did not use password_hash() to insert the password in the database your check using password_verify() attempt here:

    if(count($results) > 0 && password_verify($password, $results['password'])){
    

    will always fail because the function expects a plain password to compare against the hashed value of the password. for more insight on PHP's password functions read this post.


    In addition you may find yourself wanting to limit passwords and you really shouldn't do that.


    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。