登录页面未获得正确的信息

时间:2019-05-12 02:35:19

标签: php mysql mysqli login

我正在学习一些代码,所以我知道其中存在很多错误,但是由于某些原因,我的代码将无法获得正确的登录信息,你们可以帮我吗?用户名是spore310,密码是sloth45。

<?php
// Initialize the session
session_start();

// Check if the user is already logged in, if yes then redirect him to 
welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: index.php");
exit;
}

// Include config file
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "storm";

//create connection
$conn = new mysqli($servername, $username, $password, $dbname);

//check connection 
if($conn->connect_error){
die("Connection Failed: " . $conn->connect_error);

} 

// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

 // Check if username is empty
 if(empty(trim($_POST["username"]))){
    $username_err = "Please enter username.";
 } else{
    $username = trim($_POST["username"]);
 }

 // Check if password is empty
 if(empty(trim($_POST["password"]))){
    $password_err = "Please enter your password.";
 } else{
    $password = trim($_POST["password"]);
 }

 // Validate credentials
 if(empty($username_err) && empty($password_err)){
    // Prepare a select statement
    $sql = "SELECT id, username, password FROM user WHERE username = ?";

    if($stmt = $conn->prepare($sql)){
        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("s", $param_username);

        // Set parameters
        $param_username = $username;

        // Attempt to execute the prepared statement
        if($stmt->execute()){
            // Store result
            $stmt->store_result();

            // Check if username exists, if yes then verify password
            if($stmt->num_rows == 1){                    
                // Bind result variables
                $stmt->bind_result($id, $username, $hashed_password);
                if($stmt->fetch()){
                    if(password_verify($password, $hashed_password)){
                        // Password is correct, so start a new session
                        session_start();

                        // Store data in session variables
                        $_SESSION["loggedin"] = true;
                        $_SESSION["id"] = $id;
                        $_SESSION["username"] = $username;                            

                        // Redirect user to welcome page
                        header("location: index.php");
                    } else{
                        // Display an error message if password is not valid
                        $password_err = "The password you entered was not 
valid.";
                    }
                }
            } else{
                // Display an error message if username doesn't exist
                $username_err = "No account found with that username.";
            }
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }

    // Close statement
    $stmt->close();
}

// Close connection
$conn->close();
}
?>

我希望能够拥有一个从表中获取数据并将其用于登录用户的登录名。

0 个答案:

没有答案