如果页面刷新,我想防止会话增加。我试图检查是否已经设置了POST,例如if (isset($_POST)){$_SESSION['page']++;}
,但是它不能解决问题,因为必须多次发送该表单。
我希望$ _SESSION ['page'] ++仅在我从表单提交数据时才递增,而不是在刷新页面时才递增。
编辑:我发布了整个代码,因为不清楚它是如何工作的。
[重要提示:显然,POST / REDIRECT / GET方法不起作用,因为在发送数据后,我不需要将用户重定向到另一个页面。相反,我需要重新加载同一页面。不要认为我的话是理所当然的。
//Start using session-variables.
session_start();
//Game is started.
if (!isset($_SESSION['page']))
{
$_SESSION['page']=1;
}
else
{
//Game is on, read answer from previous page.
$answer=$_POST['answer'];
}
//Figure out what to display.
switch ($_SESSION['page'])
{
case 1: //First page of the game.
$picture="pictures/perch.gif";
$option1="Perch-pike";
$option2="Pike";
$option3="Perch";
$option4="Ruffe";
break;
case 2: //Second page of the game.
$picture="pictures/brownhare.gif";
$option1="Brown hare";
$option2="Wolverine";
$option3="Arctic hare";
$option4="Badger";
$_SESSION['answer1']=$answer;
break;
case 3: //Third page of the game.
$picture="pictures/moose.gif";
$option1="Reindeer";
$option2="Fallow deer";
$option3="Roe deer";
$option4="Moose";
$_SESSION['answer2']=$answer;
break;
case 4: //On last page answers are evaluated.
$points=0;
$answer3=$_POST['answer'];
if ($_SESSION['answer1']==3)
{
$points++;
}
if ($_SESSION['answer2']==1)
{
$points++;
}
if ($answer3==4)
{
$points++;
}
print "
<h2>Your score is $points</h2>";
print "
<a href='quiz.php'>New game</a>
<br>";
print "
<a href='../index.htm'>Back to examples</a>
<br>";
session_destroy();
exit;
}
$_SESSION['page']++;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Huntersman's examination game</title>
</head>
<body>
<form method="post" action="quiz.php">
<center>
<h2>What is this?</h2>
<table border=1>
<tr>
<td width="300">
<img src="
<?= $picture?>">
</td>
<td>
<table>
<tr>
<td>
<?= $option1?>
</td>
<td>
<input type="radio" name="answer" value="1" checked>
</td>
</tr>
<tr>
<td>
<?= $option2?>
</td>
<td>
<input type="radio" name="answer" value="2">
</td>
</tr>
<tr>
<td>
<?= $option3?>
</td>
<td>
<input type="radio" name="answer" value="3">
</td>
</tr>
<tr>
<td>
<?= $option4?>
</td>
<td>
<input type="radio" name="answer" value="4">
</td>
</tr>
<tr>
<td>
</table>
</td>
</tr>
</table>
<br>
<input type="submit" value="Answer">
</center>
</form>
</body>
</html> ```
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Huntersman's examination game</title>
</head>
<?php
//Start using session-variables.
session_start();
//Game is started.
if (!empty($_POST['page']))
{
$page = $_POST['page']+1;
$answer=$_POST['answer'];
}
else
{
//Game is on, read answer from previous page.
$page=1;
}
//Figure out what to display.
switch ($page)
{
case 1: //First page of the game.
$picture="pictures/perch.gif";
$option1="Perch-pike";
$option2="Pike";
$option3="Perch";
$option4="Ruffe";
break;
case 2: //Second page of the game.
$picture="pictures/brownhare.gif";
$option1="Brown hare";
$option2="Wolverine";
$option3="Arctic hare";
$option4="Badger";
$_SESSION['answer1']=$answer;
break;
case 3: //Third page of the game.
$picture="pictures/moose.gif";
$option1="Reindeer";
$option2="Fallow deer";
$option3="Roe deer";
$option4="Moose";
$_SESSION['answer2']=$answer;
break;
case 4: //On last page answers are evaluated.
$points=0;
$answer3=$_POST['answer'];
if ($_SESSION['answer1']==3)
{
$points++;
}
if ($_SESSION['answer2']==1)
{
$points++;
}
if ($answer3==4)
{
$points++;
}
print "
<h2>Your score is $points</h2>";
print "
<a href='quiz.php'>New game</a>
<br>";
print "
<a href='../index.htm'>Back to examples</a>
<br>";
session_destroy();
exit;
}
// $_SESSION['page']++;
?>
<body>
<form method="post" action="">
<center>
<h2>What is this?</h2>
<table border=1>
<tr>
<td width="300">
<img src="
<?= $picture?>">
</td>
<td>
<table>
<tr>
<td>
<?= $option1?>
</td>
<td>
<input type="radio" name="answer" value="1" checked>
</td>
</tr>
<tr>
<td>
<?= $option2?>
</td>
<td>
<input type="radio" name="answer" value="2">
</td>
</tr>
<tr>
<td>
<?= $option3?>
</td>
<td>
<input type="radio" name="answer" value="3">
</td>
</tr>
<tr>
<td>
<?= $option4?>
</td>
<td>
<input type="radio" name="answer" value="4">
</td>
</tr>
<tr>
<td>
</table>
</td>
</tr>
</table>
<br>
<input type="hidden" name="page" value="<?php echo $page; ?>">
<input type="submit" value="Answer">
</center>
</form>
</body>
</html>