Basically a PHP script calls the first question out of a database, the user chooses an answer, presses Submit which submits the page to itself an increments the question number by 1 to call out the next question from the database. At the same time the answer to the previous question gets assigned to a session variable corresponding to that question number.
No problem with getting the session to work, and nine times out of ten I can log in and get all the way through the test but occasionally, occasionally at no particular point in the script the session just vanishes.
I read on another post that by default they expire after 1440 seconds but I don't think thats the problem here as it can happen much earlier than that and I've sometimes taken longer without problems.
Is it anything to do with the cache-control I put at the top of the script?
Sometimes after the session is started on the previous script I can arrive on the actual test page to find no questions because the session has gone wrong. I've tried to recreate the error numerous times but am yet to find a specific cause. Obviously this script (probably in more ways than this one!) presents an unacceptable risk for a student trying to take an assessment if their test suddenly forgets who, what where and why with no way to recover the lost session!.....
Please...if anyone has any suggestions... I can't cope with spending another 8 hours on this one problem!
Here's the relevant bits of code incase it helps:
Code: Select all
<?php
session_start();
header("Cache-control: private");
mysql_connect("$server", "$user") or die(mysql_error());
mysql_select_db("keyskills_tests") or die (mysql_error());
$test_no = $_SESSIONї'test_no']; // Session variable to remember what test it is the user is taking.
if ($Submit == "Submit") {
$_SESSIONї'answer_'.$q] = $options; //records the value of the option choice to the session variable
$q_no = $q +1;
} //corresponding to the question number. The hidden field recording the question number of the previous question increments by one to load the next question.
if ($q_no == ""){
$q_no = "1";
}
else if ($q_no > $_SESSIONї'total_q']) { // Alerts the user they have reached the end of the test by comparing their current question number with the session variable total questions initialized at the beginning. (another script)
echo "You have reached the end of the test. Use the buttons to repeat questions, or Finish to end the test.";
$q_no = $_SESSIONї'total_q'];
}
$query = "select * from questions where test_no = '$test_no' and question_no = '$q_no'";
$result = mysql_query($query) or die(mysql_error()); // if there are still questions left then it selects them from the database using the test number and the current value of $q_no.
$row = mysql_fetch_array($result);
?>
</font></td>
</tr>
<tr valign="top">
<td width="662">
<table width="601" border="0" align="left" cellpadding="0" cellspacing="0">
<tr>
<td width="597" valign="top"> <div align="left">
<?php if ($rowї'diagram'] !="") { // specifies whether to load a diagram or not.
echo "<img src='diagram/",$rowї'diagram'],"' ></div></td>";
} else {
echo "<font face='Arial, Helvetica, sans-serif'>There is no diagram for this question</font>";
} ?>
</div></tr>
</table></td>
</tr>
<tr valign="top">
<td><br><font face="Arial, Helvetica, sans-serif"><b> Question <? echo $rowї'question_no'];?>:
</b><br>
<?php echo $rowї'question']; ?></font> <form name="answer" method="post" action="exam.php" >
<p><font face="Arial, Helvetica, sans-serif">
<?php
// creates the four options which will record to a variable named "options"
echo "<input type='radio' name = 'options' value='A'>", $rowї'option_a'],"<br>";
echo "<input type='radio' name = 'options' value='B'>", $rowї'option_b'],"<br>";
echo "<input type='radio' name = 'options' value='C'>", $rowї'option_c'],"<br>";
echo "<input type='radio' name = 'options' value='D'>", $rowї'option_d'],"<br>";
if ($rowї'option_e'] != "") {
echo "<input type='radio' name = 'options' value='E'>", $rowї'option_e'],"<br>";
}
?>
</font> </p>
<p>
<input type="hidden" name="q" value="<? echo $q_no;?>">
<input type="submit" name="Submit" value="Submit">
</p>
</form>