This is what index.php looks like, where the user registers:
<title>php
<h3>Registration</h3>
Login
Password
Repeat password
Registration
<a href="login.php">Already registered?</a>
0) {
$error = "A user with this login already exists!";
}
if (!$error) {
session_unset();
$_SESSION['username'] = $login;
$query = "INSERT INTO `users` (`id`, `login`, `password`, `admin`, `points`) VALUES (NULL, '$login', '$password1', 0, 0);" ;
mysqli_query($link, $query);
echo "You have successfully created a user!";
header("Location: test.php");
exit();
} else {
echo $error;
}
}
?>
test.php:
<title>Testing
Hello, $username<br>";
} else {
header("Location: index.php");
exit();
}
$dateAndTime = date("d.m.Y H:i");
if ($_SESSION[$username]['selected_questions'] === null) {
$query = "SELECT * FROM questions ORDER BY RAND() LIMIT 5";
$result = mysqli_query($link, $query);
$elapsed_time = 0;
$selected_questions = array();
while ($row = mysqli_fetch_assoc($result)) {
$selected_questions[] = $row;
}
$_SESSION[$username]['selected_questions'] = $selected_questions;
$_SESSION[$username]['test_start_time'] = time();
echo "<a>Start of test $dateAndTime, maximum test time 10 minutes</a>";
}
?>
$question<p></p>";
echo "$answer1<br>";
echo "$answer2<br>";
echo "$answer3<br>";
}
}
?>
Check answers
";
$total_score = 0;
$query = "SELECT * FROM questions";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)) {
$question_id = $row['id'];
$correct_answer = $row['correct_answer'];
if (isset($_POST["question_$question_id"])) {
$user_answer = $_POST["question_$question_id"];
if ($user_answer == $correct_answer) {
$total_score++;
}
}
}
if ($elapsed_time > $max_allowed_time) {
echo "The test time exceeded the allowed limit. The results were cancelled.<br>";
$total_score = 0;
}
$update_query = "UPDATE users SET points = $total_score WHERE login = '$username'";
mysqli_query($link, $update_query);
echo "You scored $total_score out of a possible 5 points.";
$_SESSION['points'] = $_SESSION['points'] + $total_score;
$_SESSION[$username]['test_start_time'] = time();
$elapsed_time = 0;
}
?>
<a href="index.php">Return to home</a>
What I have tried:
Each user should be randomly selected 5 questions from the database. How do I record a user's questions in a session so that each user has their own questions and they don't change after the page is refreshed?