[英]How do I use a $_SESSION['variable'] from one session in another?
我正在制作一個Web應用程序,用戶可以在其中登錄並訪問個人資料並進行測驗。 我大多數情況下都起作用,唯一的問題是,它似乎“忘記”了哪個用戶登錄。這意味着我無法從用戶登錄會話時訪問任何變量。 例如,我有一個$_SESSION['username'] = $username;
當我嘗試在其他會話或頁面中使用變量$username
,該變量返回未識別的變量。 另外,我還沒有終止登錄會話。
現在,我正嘗試將測驗的結果與用戶的用戶名一起存儲到數據庫中,但它僅存儲得分而不是用戶名。
下面是我的代碼。
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拋出異常,例如“已發送標頭”或“無法啟動會話”等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.