Hi all,

Im very new to php. Currently im creating an online exam system which uses php and mysql as the database. I have registration and login page which works pretty fine. I had problem when it comes to question page. I have about 20 questions. As soon the user login to the system, he should start answering. The system should remember the number of correct answers til the 20th question. I used session to hold the value of number of correct answers. For example :

Login.php  login and redirect to ques1.php

ques1.php  first question, click submit to redirect to ques2.php

ques2.php  second question. Click submit to go to ques3.php or click back to return to ques1.php

The problem is when I click back button the counter which holds the value of correct answer doesn’t work properly.
Let say I’ve answered 3 questions correctly, the counter should show 3, then if click back button, it suppose become 2 but it doesn’t happen here. The counter increase becomes 4.

Code sample as below….

ques1.php

<html>
<head>
<title>Question 1 </title>
</head>
<body>
<form action="ques2.php" method="GET">
<font size="3" face="Verdana">
<?php
session_start();
echo "Current User : ".$_SESSION['name'];
?>
<br><br><b>Question 1</b><br><br>
</font>
<font size="2" face="Verdana">
What is HTML?<br><br>
<input type="radio" name="ques1" value="a"> Hyper Text Markup Language<br>
<input type="radio" name="ques1" value="b"> Hyper Transfer Markup Language<br>
<input type="radio" name="ques1" value="c"> Hyper Text Transfer Language<br><br>
<INPUT TYPE=SUBMIT VALUE="submit">
</form>
</body>
</html>

ques2.php

<html>
<head>
<title>Question2</title>
</head>
<body>
<form action="ques3.php" method="GET">
<font size="3" face="Verdana">
<br><br><b>Question 2</b><br><br>
</font>
<font size="2" face="Verdana">
What is PHP?<br><br>
<input type="radio" name="ques2" value="a">Personal Home Page<br>
<input type="radio" name="ques2" value="b"> PHP Hypertext Preprocessor<br>
<input type="radio" name="ques2" value="c"> Personal Hypertext Processor<br><br>
<INPUT TYPE=SUBMIT VALUE="submit">
<input type="button" name="back" value="Back" onClick="history.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start();
$counter=0;
$answer1=$_GET['ques1'];

$result=mysql_query("select ans from answer where qid='q1'");
$res=mysql_fetch_array($result);

if($answer1==$res["ans"])
{
	$counter=$counter+1;
	$_SESSION['ans']=$counter;
}
else
{
$_SESSION['ans']=$counter;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESSION['ans'];
?>
</body>
</html>

ques3.php

<html>
<head>
<title>Question3</title>
</head>
<body>
<form action="ques4.php" method="GET">
<font size="3" face="Verdana">
<br><br><b>Question 3</b><br><br>
</font>
<font size="2" face="Verdana">
What is MySQL?<br><br>
<input type="radio" name="ques3" value="a">Markup Languange<br>
<input type="radio" name="ques3" value="b"> Programming Language<br>
<input type="radio" name="ques3" value="c"> Database<br><br>
<INPUT TYPE=SUBMIT VALUE="submit">
<input type="button" name="back" value="Back" onClick="history.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start();
$answer2=$_GET['ques2'];
//$counter=$_SESSION['ans'];

$result=mysql_query("select ans from answer where qid='q2'");
$res=mysql_fetch_array($result);

if($answer2==$res["ans"])
{
	//$counter=$counter+1;
	$_SESSION['ans']++;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESSION['ans'];
?>
</body>
</html>

I even have tried to add header to stop the browser from cache the pages but it doesn't work. What should I change in my code?

Thank you.

You could try checking the referring page before incrementing the Session counter. Obviously you'll have to change the url to the one you are looking for:

if($answer1==$res["ans"] && $_SERVER['HTTP_REFERER']=="http://localhost/ques2.php")
{
$_SESSION['ans']++;
}

just make a back button. and use some php to take the session variable and decrease it by one. then redirect to the correct page.

This article has been dead for over six months. Start a new discussion instead.