Hi guys. I have a problem in my web based examanation system written in php.
Here's the scenario:
when a user takes an exam. 1 question is displayed 1 at a time. my problem is if he/she is at the 2nd question then he/she pressed the back button he/she is redirected back to the 1st question and my system just go crazy. How can i prevent the user from pressing the back button or returning to the previous questions? Please I really need help on this. Can anyone give me suggestion for possible solutions on my problem?
Thanks in advance
by the way here's my code, you can also download it from attachment.
------------------------------------------process_exam.php------------------------------------------------------------
<?php
//start session
session_start();
//declare variables
//iCorrect= variable for score
$_SESSION['iCorrect']=0;
//a=variable for iteration kang index kang mga array or serves as an current item number
$_SESSION['a']=1;
//connect to database
include('mysql_connect.php');
$result = mysql_query("select * from questions where exam_id ='$_SESSION[exam_id]'");
//count the number of found questions
$_SESSION['num_of_questions']=mysql_num_rows($result);
//declare the iterator
$i=1;
//get the options for the question1,question2 and so on and so on. depende kung ilang questions ang mafetch nya sa dbase.so pde kht ilang questions lagay ng user
while ($row = mysql_fetch_assoc($result))
{
$questions[$i] = $row['question'];
$op1[$i] = $row['op1'];
$op2[$i] = $row['op2'];
$op3[$i] = $row['op3'];
$op4[$i] = $row['op4'];
$answers[$i]=$row['answer'];
$i++;
}
//view the data's in the array for debugging purposes
echo 'answers: ';
print_r($questions);
echo '<br>';
print_r($op1);
echo '<br>';
print_r($op2);
echo '<br>';
print_r($op3);
echo '<br>';
print_r($op4);
echo '<br>';
print_r($answers);
echo '<br>';
echo '<br>';
//pass the arrays to session arrays to be used in other php pages
$_SESSION['question']=$questions;
$_SESSION['op1']=$op1;
$_SESSION['op2']=$op2;
$_SESSION['op3']=$op3;
$_SESSION['op4']=$op4;
$_SESSION['answers']=$answers;
//print the arrays. debugging purposes
//1 is the starting index of the array because the $i is equal to 1
print_r($_SESSION['question']);
echo '<br>';
print_r($_SESSION['op1']);
echo '<br>';
print_r($_SESSION['op2']);
echo '<br>';
print_r($_SESSION['op3']);
echo '<br>';
print_r($_SESSION['op4']);
echo '<br>';
print_r($_SESSION['answers']);
header("location:exam.php");
?>
</body>
</html>
----------------------------------------------------------exam.php--------------------------------------------------------------------
<?php
//start session
session_start();
echo '<br>';
echo '<br>';
echo '<tr>';
echo '<b>';
echo 'Question: ';
echo '<td align = center>'.$_SESSION['question'][$_SESSION['a']].'</td>';
echo '</tr>';
echo '<form method = "post" action= "nxt_question.php">';
echo '<table border = "0">';
echo '<tr>';
//echo '<td>';
echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op1'][$_SESSION['a']]."'>";
echo '<td>'.$_SESSION['op1'][$_SESSION['a']].'</td>';
echo '</tr>';
echo '<tr>';
echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op2'][$_SESSION['a']]."'>";
echo '<td>'.$_SESSION['op2'][$_SESSION['a']].'</td>';
echo '</td>';
echo '</tr>';
echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op3'][$_SESSION['a']]."'>";
echo '<td>'.$_SESSION['op3'][$_SESSION['a']].'</td>';
echo '</tr>';
echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op4'][$_SESSION['a']]."'>";
echo '<td>'.$_SESSION['op4'][$_SESSION['a']].'</td>';
echo '</tr>';
echo '</table>';
echo '<tr>
<td>
<input type = "submit" value="Next Question">
</td>
</tr>';
echo '</form>';
?>
----------------------------------------------nxt_question.php-------------------------------------
<?php
//start session
session_start();
//Checking of answers kada mag pindot next question button
if(isset($_POST['choice']))
{
$ans_of_user=$_POST['choice'];
$_SESSION['answers'][$_SESSION['a']];
if (strcmp($ans_of_user,$_SESSION['answers'][$_SESSION['a']])==0)
{
$_SESSION["iCorrect"]++;
}
}
//add 1 to the session a. session a is the iterator. serves as indexes of the arrays that stores the questions,answers etc...
$_SESSION['a']++;
//ridirect to exam.php if there are still number of items remaining.
while($_SESSION['a']<=$_SESSION['num_of_questions'])
{
header("location:exam.php");
exit(); //kill the script
}
echo 'exam finished!';
echo '<br>';
echo 'Your score is: ';
echo $_SESSION["iCorrect"];
//unset the session variables needed to be reset
unset($_SESSION["iCorrect"]);
unset($_SESSION["exam_id"]);
unset($_SESSION["invitation_code"]);
unset($_SESSION["a"]);
unset($_SESSION["num_of_questions"]);
unset($_SESSION["question"]);
unset($_SESSION["op1"]);
unset($_SESSION["op2"]);
unset($_SESSION["op3"]);
unset($_SESSION["op4"]);
unset($_SESSION["answers"]);
?>
18166_.php
18167_.php
18168_.php