我正在制作一个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'];
}
?>
对不起,如果我犯了一个非常简单的愚蠢错误,不要恨我,我在编码方面仍然很糟糕。
答案 0 :(得分:2)
将session_start();
放在代码的最顶部,例如,在final.php文件的最顶部,而不是process.php文件。
例如;
<?php
session_start();
include 'database.php';
?>
答案 1 :(得分:0)
您可以尝试的简单解决方案
session_start();
我们必须将其添加到php文件的顶部,否则php抛出诸如“已发送标头”或“无法启动会话”之类的异常。