I'm using:
php 4.2.3
Apache 1.3.27
Os: Windows XP Home Edition
Packaged and setup by phpdev 4.2.3 from firepages.com.au
Errors: error_reporting = E_ALL & ~E_NOTICE
display_errors = On
display_startup_errors = On
log_errors = On
error_log = errors.txt
track_errors = Off
I put together two smaller examples one of which has the problem and another which works fine. The problem seems to be caused by my redirecting to one page, assigning values to session array, and then redirecting back again, though I can't see why this causes difficulty, nor why the later echo statement affects the action of the script.When I leave out this redirection in the smaller example it works.
Start at problem_page, which redirects to setup_page and back again to problem page. The difficulty occurs then in problem_page when I ask it to redirect to redirect.php if the question number is >= the question array size. It's like the session array gets wiped out.
I've attached everything aswell as copying it here - not sure which way you prefer.
One that doesn't work:
<?php
//Problem_page.php - starting point
//This page should perform tasks related to asking quiz questions
//Problem lies within if statement to redirect when at the end of question list
//$num contains the question number and $count the number of questions
session_start();
//This is how I populated my questions in the session array if we arrive at this page by mistake.
//Not essential for this example - but put it in in case it has something to do with the problem.
if (!isset($_SESSION['questions'])){header('Location: setup_page.php');}
//Give values to $num and count the questions in session array
$num=1;
$count=count($_SESSION['questions']);
//This next echo seems to affect how the 'if' statement works. When commented out the page redirects and the count is 0.
//When it's included, everything works as expected. If there's something else other than the header statement in the
//'if' block, everything still works as normal. (Note I would prefer not to have to do this as I'm using CSS and
//all my page content should get prepared and displayed later.)
//echo "<h1>Not redirected</h1><p>num=$num<br>count=$count<p>";
if ($num >= $count) {header ("Location: redirect.php?num=$num&count=$count");}
//The following is an alternative to the header statement - it works as expected
//if ($num >= $count) {echo "<br> num is greater than count<br>";}
//else echo "<br>Num is less than count<br>";
echo "Everything ok";
//Just to close session and try again for testing each version of the script
echo "<br><a href=\"close_session.php\">Close the session</a>";
?>
<?php
//Setup_page.php
//Redirected here from problem_page if the session question array isn't populated
//Fill the array with questions and send it back again
session_start();
function get_questions() {
//My function gets questions from database - here's a simple one to populate the session array
$array=array("name"=>"mrob",
"questions"=>array(1=>'What is wrong?', 2=> 'Why does it not work?', 3=>'Php is great,no?'));
$_SESSION=$array;
}
get_questions();
//Session populated with questions - send back to where it came from - problem_page
header ('Location: problem_page.php');
?>
<?php
session_start();
//Redirect.php
//shouldn't be here unless num >= count (ie. run out of questions)
//Check the values of num and count passed by the redirect from problem_page.php
echo "<h1>Redirected</h1><p>name=".$_SESSION['name']."<br>num=".$_GET['num']."<br>count=".$_GET['count']."</p>";
//Just to close session and try again for testing each version of the script
echo "<br><a href=\"close_session.php\">Close the session</a>";
?>
<?php
session_start();
unset($_SESSION['name']);
unset($_SESSION['questions']);
// kill session variables
$_SESSION = array(); // reset session array
session_destroy(); // destroy session.
echo "<a href=\"problem_page.php\">Try again</a>";
?>
Oops! Just realised I can't attach php files.
Anyway here's the shorter version without the first redirect, which works as it should,(setup_page is gone and redirect.php and close_session.php are as above):
<?php
//Problem_page.php - starting point
//This page should perform tasks related to asking quiz questions
//No problem with this page - works as expected regardless of echo being commented out or not
session_start();
//Populate the question array in session without any redirection
$array=array("name"=>"mrob",
"questions"=>array(1=>'What is wrong?', 2=> 'Why does it not work?', 3=>'Php is great,no?'));
$_SESSION=$array;
//Give values to $num and count the questions in session array
$num=1;
$count=count($_SESSION['questions']);
//This next echo is the one I used to comment out - but it works in this version smaller_session_test as it should
//echo "<h1>Not redirected</h1><p>num=$num<br>count=$count<p>";
if ($num >= $count) {header ("Location: redirect.php?num=$num&count=$count");}
//The following is an alternative to the header statement - it works as expected
//if ($num >= $count) {echo "<br> num is greater than count<br>";}
//else echo "<br>Num is less than count<br>";
echo "Everything ok";
//Just to close session and try again for testing each version of the script
echo "<br><a href=\"close_session.php\">Close the session</a>";
?>