Why can't my script 'see' a SESSION variable?

I’m having a nightmare trying to create a variable (GET, POST or preferably SESSION) in an included file that can then be read by a control script when the included file exits (without ‘submit’). Nothing I’ve tried has worked so far, yet I can’t believe it’s not possible. I’m probably missing something small but vital.

In summary, I have an e-mail form which is called by the visitor clicking on a link which is a URL with query string (e.g. http://example.com/index.php?id=1234&name=myname). The control script filters the GET array, and loads the form. On submission the control script will reload the form (it still has the same query), which then checks for errors in the entries. If there are errors, the form is re-displayed. If there are no errors, the form script will extract e-mail addresses from MySQL, and send the e-mail(s) AND THEN EXIT (back to the control script). Everything works fine to that point.

The form action is ‘action=“”’, which returns the form complete with the original query string. I’m aware that ‘action=“?”’ would clear the query string, but then the control script wouldn’t return the form on submit (and, worse, I’d lose the data).

I thought it would be simple to set a SESSION variable if no errors were found, and read this variable on return to the control script, so that a ‘Success’ page could be shown. But the script doesn’t ‘see’ it, although it’s there when I view the session file in my editor.

There’s a lot of code involved, most of it working correctly, and nothing to do with the problem. Here are the bits I consider relevant:
Extract from control script (index.php):


if (isset($_GET['id']) && isset($_GET['name'])) {
	if (isset($_SESSION['noerrors']) && $_SESSION['noerrors'] == "YES") {
		$content = $_SERVER['DOCUMENT_ROOT'] . '/inc/msgsent.inc.htm';
		include $_SERVER['DOCUMENT_ROOT'] . '/templates/pagetemplate.html.php';
		unset ($_SESSION['noerrors']);
		exit();
	} else {
	$_POST['action'] ='E-mail Shortlist';	// line 114
	}
}
...

//	SEND e-mail to Short-list (or single listing)
//	This segement selects the e-mail address(es) from DB and presents the form for completion
if (isset($_POST['action']) && $_POST['action'] == 'E-mail Shortlist') {
	$content = $_SERVER['DOCUMENT_ROOT'] . '/inc/mailformlogic.php';
	include $_SERVER['DOCUMENT_ROOT'] . '/templates/pagetemplate.html.php';
	exit();
}


The first block of code above SHOULD load the ‘Success’ page if the SESSION variable ‘noerrors’ is present, but this is the bit that doesn’t happen. ELSE it allows the script to continue to the second block, where the e-mail form is loaded.

The e-mail form itself (mailformlogic.php) has the following relevant parts:


if (isset($_POST['send'])) {  //if the form has been submitted...
	...check required fields and sanitise input...
		if (count($errors) > 0) {  // if there are errors
			...re-display form with error messages, for correction...
		} else {	// send the e-mails
			...prepare HTML and text e-mails...
			...send e-mails...
                        $_SESSION['noerrors'] = "YES";
			exit();
		}
}  // end of 'if form submitted'

  // display the form either first time round, or if there are errors
	...display the form for completion...


The script exits OK, but won’t display the ‘Success’ page, apparently because it can’t see the new SESSION variable ‘noerrors’.

I suspect it’s something to do with when the contents of the Session file are refreshed, as I can access other SESSION variables created earlier.
You can see all this at:
http://holidaymullandiona.com/index.php?id=2073&name=Maolbhuidhe+Bed+%26+Breakfast

Note: Actual sending of e-mails is disabled in this example.

Can anyone suggest what I need to do, please ?

What if you var_dump($_SESSION); on the line after session_start()? Do you get a value?

I got a partially broken page when I submitted the form(I see the nav and stuff, but theres nothing in your content area). Could be that this block of code is executing, but has issues. Last bit of output is <div id=“txtcol”>


	if (isset($_SESSION['noerrors']) && $_SESSION['noerrors'] == "YES") {
		$content = $_SERVER['DOCUMENT_ROOT'] . '/inc/msgsent.inc.htm';
		include $_SERVER['DOCUMENT_ROOT'] . '/templates/pagetemplate.html.php';
		unset ($_SESSION['noerrors']);
		exit();
	} else {

Forget includes for a moment. Just echo stuff out to help you debug.

Thanks. I get the broken page too, as if it’s trying to load something. If you refresh that page you do get the required result. It seems to need the refresh to pick up the SESSION variable ‘noerrors’, but why I’m not clear. (If I was I’d probably have solved it)

I’ve already tried echoing the variables as you suggest (but not the var dump, I’ll do that tomorrow). I didn’t upload it as I thought it might add to the confusion. The actual code is below. The purpose of the ‘z’ variable stuff is to record how many times the form is displayed (or how many times the script passes through that point). It’s a bit clumsy, but meets the need.

I’ve uploaded it so you can see it in action.


if (isset($_GET['id']) && isset($_GET['name'])) {
	if (isset($_SESSION['z'])) {
		$z = $_SESSION['z'];
		$z++;
		$_SESSION['z'] = $z;
	} else {
		$z = 1;
		$_SESSION['z'] = 1;
	}
	echo "Form presentation " . $z . "<br />";

	if (isset($_SESSION['noerrors']) && $_SESSION['noerrors'] == "YES") {
		echo $_SESSION['noerrors'];
	} else {
		echo "'No Errors' SESSION variable not set (or not seen)";
	}
	if (isset($_SESSION['noerrors']) && $_SESSION['noerrors'] == "YES") {
		$content = $_SERVER['DOCUMENT_ROOT'] . '/inc/msgsent.inc.htm';
		include $_SERVER['DOCUMENT_ROOT'] . '/templates/pagetemplate.html.php';
		unset ($_SESSION['noerrors']);
		exit();
	} else {

//}
	$_POST['action'] ='E-mail Shortlist';	// line 114
	}
}

In my localhost version I can delete the SESSION variables and start again, but you won’t be able to do that in the live test, which may produce some strange results unless you close the session or refresh the broken page so as to unset the ‘noerrors’ Session var.

It’s late in Scotland, so I’ll look at this again in the morning.

Later: var_dump($_SESSION) confirms what was becoming obvious. $_SESSION[‘noerrors’] isn’t visible to the script until the ‘broken’ page is refreshed.

I can’t think what to do to rectify this. Any ideas gratefully recieived !

I’ve at long last realised what the problem is here, but I’m no nearer a solution, which may require a different approach.
In brief, the execution returns to the control script the moment the form is submitted, so the criteria for the next page are established at this point (before $_SESSION[‘noerrors’] is created).