簡體   English   中英

PHP會話登錄返回錯誤的用戶名和密碼

[英]PHP Session Login return wrong username and password

嘗試登錄到我的站點時,腳本使用錯誤的用戶名或密碼返回Im。

index.html的標題:

<?php
session_start();
require_once 'classes/Membership.php';
$membership = new Membership();
if ($_POST && !empty ($_POST['username']) && !empty ($_POST['password'])) {
$response = $membership->validate_user($_POST['username'], $_POST['password']);
}
?>

index.html中的表格:

    <div class="login" id="register">
     <form method="POST" action="" accept-charset="utf-8">
      <fieldset>
       <p>
        <label for="username">Username</label><br />
        <input type="text" name="username" value="" id="username">
       </p>
       <p>
        <label for="password">Password</label><br />
        <input type="password" name="password" value="" id="password">
       </p>
      </fieldset>
      <input type="submit" class="button" value="Sign in">
     </form>
    </div>
<?php if(isset($response)) echo "<p class = 'error-msg'>" .$response.".</p>"; ?>

會員資格

<?php
require 'MySQL.php';

    class Membership
    {
        function validate_user($username, $password)
        {
            $mysql = new MySQL();
            $ensure_credentials = $mysql->verify_in_db($username,md5($password));

            if ($ensure_credentials)
            {
                $_SESSION['status'] = 'authorized';
                header("location: index_member.php");
            }
            else return "Please enter a correct username and password";
        }
    }

MySQL.php

<?php
require_once 'includes/constants.php';

    class MySQL
        {
            private $conn;

            function __construct()
            {
             $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) 
             or die ('There was a problem connecting to the database.');
            }

            function verify_in_db($username, $password)
            {
                $query = "SELECT * 
                                FROM users
                                WHERE username = ? AND password = ?
                                LIMIT 1";
                if ($stmt = $this->conn->prepare($query))
                {
                $stmt->bind_param('ss', $username, $password);
                $stmt->execute();

                    if ($stmt->fetch())
                    {
                        $stmt->close();
                        return true;
                    }
                }
            }
        }

constants.php

<?php
// Define constants

define ('DB_SERVER', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_NAME', 'membership');

沒有數據庫密碼,請不要混淆。 xampp上的Standart DB。 我嘗試使用的憑據也是100%正確,sql常量也是如此。

可能是因為您所說的密碼。 它的

'password'

但是由於MD5,應該是這樣的

'fglsdgjdfnfjlsmg905760657604657604jtvymwy'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM