简体   繁体   中英

PHP Session Login return wrong username and password

When trying to log in to my site, the script returns Im using a wrong username or password.

Head of 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']);
}
?>

Form in 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>"; ?>

Membership.php

<?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');

There is no db password, dont be confused. Standart DB on xampp. The credentials I try to use are 100% correct, the sql constants too.

maybe because of your password as you said. its

'password'

but it should be something like this because of MD5

'fglsdgjdfnfjlsmg905760657604657604jtvymwy'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM