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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化