如何在一个会话中使用另一个会话中的 $_SESSION['variable']?

How do I use a $_SESSION['variable'] from one session in another?

我正在制作一个网络应用程序,用户可以在其中登录并能够访问个人资料并参加测验。我已经完成了大部分工作,唯一的问题是,它似乎 'forget' 哪个用户已登录。我的意思是我无法访问用户登录会话时的任何变量。 例如,当我尝试在不同的会话或页面中使用变量 $username 时,我有一个 $_SESSION['username'] = $username; which returns 未识别的变量。另外,我还没有终止我的登录会话。

现在我正在尝试将我的测验结果与用户的用户名一起存储到数据库中,但它只存储分数而不存储用户名。

下面是我的代码。

authenticate.php 文件(包含关于用户名的变量)

<?php
session_start();
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'phplogin';
// Try and connect using the info above.
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ( mysqli_connect_errno() ) {
    // If there is an error with the connection, stop the script and display the error.
    die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data was submitted, isset will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
    // Could not get the data that should have been sent.
    die ('Username and/or password does not exist!');
}
// Prepare our SQL
if ($stmt = $con->prepare('SELECT username, password FROM users WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    $stmt->store_result();
    // Store the result so we can check if the st_account exists in the database.
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($username, $password);
        $stmt->fetch();
        // st_account exists, now we verify the password.
        if (password_verify($_POST['password'], $password)) {
            // Verification success! User has loggedin!
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['username'];
            $_SESSION['username'] = $username;
            include_once 'homepage.php';

            // echo 'Welcome ' . $_SESSION['name'] . '!';
        } else {
            echo 'Incorrect username and/or password!';
        }
    } else {
        echo 'Incorrect username and/or password!';
    }
    $stmt->close();
} else {
    echo 'Could not prepare statement!';
}
?>

final.php 文件

<php include "process.php"?>

第 24 - 44 行

    <main>
        <div class="container">
             <h2>You are Done!</h2>
             <p>Congrats! You have completed the test</p>
             <p>Final score: <?php echo $_SESSION['score']; ?></p>
           <?php echo $score; ?>
             <a href="question.php?n=1" class="start">Take Test Again</a>
           <?php
           $DB_HOST = 'localhost';
           $DB_USER = 'root';
           $DB_PASS = '';
           $DB_NAME = 'phplogin';
           $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
            $query = "INSERT INTO `results`(`username`,`score`) VALUES ($username, $score)";
            mysqli_query($con, $query);
            ?>



             <?php session_destroy(); ?>
        </div>

我不知道是否有必要包含 process.php,但我认为显示 $score 变量的来源可能会有所帮助。

process.php 文件(这不是整个文件。)

<?php include 'database.php'; ?>
<?php session_start(); ?>
<?php
    //Check to see if score is set_error_handler
    if (!isset($_SESSION['score'])){
        $_SESSION['score'] = 0;
    }
    $score = $_SESSION['score'];

}
?>

抱歉,如果我犯了一个非常简单的愚蠢错误,请不要恨我,我的编码仍然很糟糕。

session_start(); 放在代码的最顶部,例如,放在 final.php 文件的最顶部,而不是 process.php 文件中。

例如;

<?php
    session_start();
    include 'database.php'; 
?>

您可以尝试的简单解决方案

session_start();

我们必须将其添加到 php 文件的顶部,否则 php 会抛出 'headers already sent' 或 'can’t start the session' 等异常