为什么不登录为我提供session_id()?

问题描述 投票:0回答:1

我正在构建一个简单的登录系统。注册正在使用password_default:

enter image description here

所以,现在登录。这是我的登录类:

<?php
include("../Controllers/DatabaseController.php");

class LoginModel extends DatabaseController
{

protected $dbconn;

public function __construct()
{
    $this->dbconn = DatabaseController::instance();
}

public function Login()
{

    $db = $this->dbconn->pdo;

    try {

        $username = $_POST['username'];
        $passwordAttempt = $_POST['user_password'];

        //Retrieve the user account information for the given username.
        $sql = "SELECT * FROM user WHERE username = :username";
        $stmt = $db->prepare($sql);

        //Bind value.
        $stmt->bindParam(':username', $username);

        //Execute.
        $stmt->execute();

        //Fetch row.
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        //If $row is FALSE.
        if ($user === false) {
            //Could not find a user with that username!
            ?>
            <script type="text/javascript">
                alert("username not found!");
                window.location.href = "../Views/login.php";
            </script>
            <?php
        } else {

            //User account found. Check to see if the given password matches the
            //password hash that we stored in our users table..
            $validPassword = password_verify($passwordAttempt, $user['user_password']);

            //If $validPassword is TRUE, the login has been successful.
            if ($validPassword) {

                //Provide the user with a login session.
                $_SESSION['id'] = $user['id'];
                $_SESSION['logged_in'] = time();

                //Redirect to our protected page, which we called home, to see if we are provided a session.php
                ?>
                <script type="text/javascript">
                    alert("You're logged in!");
                    window.location.href = "../index.php";
                </script>
                <?php
                header('Location: home.php');
                exit;
            } else {
                //$validPassword was FALSE. Passwords do not match.
                ?>
                <script type="text/javascript">
                    alert("Password is incorrect!");
                    window.location.href = "../Views/login.php";
                </script>
                <?php
            }
        }

    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
}
}

现在我知道,这不是合适的OOP,但我正在学习。

当我按下登录时,密码匹配:

enter image description here

但是当重定向到home.php时,登录似乎没有为我提供session_id ...

enter image description here

Home.php:

<?php
/**
* Start the session.
*/
session_start();
/**
* Check if the user is logged in.
*/
if(!isset($_SESSION['id']) || !isset($_SESSION['logged_in'])){
//User not logged in. Redirect them back to the login.php page.
?>
<script type="text/javascript">
    alert("You're not logged in!" );
</script>
<?php
exit;
}
/**
* Print out something that only logged in users can see.
*/

echo 'Congratulations! You are logged in!';

我希望有人有解决方案,因为我遗憾地看不到一个。

完成我的部分login.php:

<?php
include "../Models/LoginModel.php";

$login = new LoginModel();
?>
<?php
if (isset($_POST["submit"])) {
$login->Login();
}
?>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<title>Title</title>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="../style-registration.css">


</head>
<body>
<?php
include 'header.php';
?>
<div class="signup-form">
<form action="" method="post">

我的部分header.php:

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<title>Scores Website</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="../style-index.css">
</head>
<body>

<nav class="navbar navbar-expand-xl bg-light">
php session pdo login
1个回答
0
投票

尝试以这种结构结束:

<?php 
    include "../Models/LoginModel.php";

    session_start(); 

    if ($_POST) {
        //execute login method
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    //set title, meta, call needed css files
</head>
<body>
    //your form etc...
    //end with javascript calls
</body>
</html>

按此顺序,一切都应按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.