Session randomly drops out?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Session randomly drops out?

Post by Skittlewidth »

I just started using sessions for the first time on a prototype for an online multiple choice test.
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! :wink:

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&#1111;'test_no']; // Session variable to remember what test it is the user is taking.

	
if ($Submit == "Submit") &#123;
		$_SESSION&#1111;'answer_'.$q] = $options; //records the value of the option choice to the session variable
		$q_no = $q +1;
                                    &#125;						//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 == "")&#123;
	$q_no = "1";
	&#125;
	else if ($q_no > $_SESSION&#1111;'total_q']) &#123; // 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&#1111;'total_q'];
	&#125;
	$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&#1111;'diagram'] !="") &#123; // specifies whether to load a diagram or not.
		  					echo "<img src='diagram/",$row&#1111;'diagram'],"' ></div></td>";
							&#125; else &#123;
							echo "<font face='Arial, Helvetica, sans-serif'>There is no diagram for this question</font>"; 
							&#125; ?>
            </div></tr>
      </table></td>
  </tr>
  <tr valign="top"> 
    <td><br><font face="Arial, Helvetica, sans-serif"><b> Question <? echo $row&#1111;'question_no'];?>: 
      </b><br>
      <?php echo $row&#1111;'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&#1111;'option_a'],"<br>";
echo "<input type='radio' name = 'options' value='B'>", $row&#1111;'option_b'],"<br>";
echo "<input type='radio' name = 'options' value='C'>", $row&#1111;'option_c'],"<br>";
echo "<input type='radio' name = 'options' value='D'>", $row&#1111;'option_d'],"<br>";

 if ($row&#1111;'option_e'] != "") &#123;
echo "<input type='radio' name = 'options' value='E'>", $row&#1111;'option_e'],"<br>";
		&#125;
 ?>
 </font> </p>
 <p> 
 <input type="hidden" name="q" value="<? echo $q_no;?>">
          <input type="submit" name="Submit" value="Submit">
        </p>
      </form>
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Does anyone think it could be an issue with my local machine?

I'm sooo stuck! :(
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

From a quick glance at your code, it looks as though your not passing the session id around, is the above code in the "exam.php" file, which is the action of your form ?

Also, what version/software are you using and on what platform ?

This:

Code: Select all

if ($Submit == "Submit") { 
      $_SESSION['answer_'.$q] = $options; //records the value of the option choice to the session variable 
      $q_no = $q +1; 
}
Needs to be:

Code: Select all

if ($_POST['Submit'] == "Submit") { 
      $_SESSION['answer_'.$q] = $options; //records the value of the option choice to the session variable 
      $q_no = $q +1; 
}
If you are using one of the latest version of PHP.

Regards,
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Yeah the script is in exam.php which submits to itself each time.
The reason I'm not using $_POST is because register globals is on, although the PHP version on this server is 4.2.4.

Its on a Novell server, actually, when we'd usually use Linux.
As I said before the session only fails occasionally. this morning it failed once in about 10 attempts. Still very high though when you consider what the script is meant to be for!
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Closure

Post by Skittlewidth »

In the end I experimented by moving the script over to a Linux server and it worked fine and dandy after that. I'm going to compare the PHP configurations in a second, but we usually set them all up with exactly the same settings.

Anyway, after all that hassle, I went down to a classroom and tested the script again and lo and behold, the college has set the default privacy settings in IE so high that I can't seem to use sessions!!!

Excuse me for a second........


AAAAAAAARRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thankyou.
Post Reply