我正在创建一个在线测验系统。假设对于question1 url是/Q/Q1.php而对于question2 url是/Q/Q2.php。我不希望用户直接跳到Q2.php页面而不解决问题1.我该如何实现?
答案 0 :(得分:2)
您需要存储会话中已回答的问题。这样,您可以检查每个请求的会话,看看是否允许他们回答这个问题。
session_start(); // always start the session with each request
if (/* question was answered correctly */) {
$_SESSION['questions'][] = $question_number;
} else {
/* question was not answered correctly take action here */
}
// To check if they may proceed to the next question
if (in_array($question_number - 1, $_SESSION['questions'])) {
/* Show the next question */
} else {
echo "You didn't answer the last question yet!";
}
答案 1 :(得分:0)
您可以按照以下方式执行此操作
<强> Q1.php 强>
<html>
<p>What is stack overflow</p>
<form method="post" action="Q2.php">
<input type="radio" name="answer1" value="Website">Website<br>
<input type="radio" name="answer1" value="Software">Software<br>
<input type="submit" value="submit" name="Q1">
</form>
</html>
<强> Q2.php 强>
<?php
if($_POST['answer1']==null)
{
header('Location:Q1.php');
}
else
{
?>
<p>This is question 2</p>
<form method="post" action="Q3.php">
<input type="radio" name="answer2" checked value="Website">Website<br>
<input type="radio" name="answer2" value="Sotware">Software<br>
<input type="submit" value="submit" name="Q1">
</form>
<?php
}
?>
希望它有所帮助。