Page 1 of 1

would header() ever kill a session? - noob [solved]

Posted: Mon Jan 23, 2006 1:00 pm
by imstupid
sorry in advance. I can't seem to get a variable to pass while using sessions.

Here's the page1.php (after a user submitted a form)

Code: Select all

<?PHP
		session_start(); 
		header("Cache-control: private"); 

		if(isset($_POST["submit"] )) {
		// connect stuff here, etc. 
		// other non-important code here
		
		$refnumber= // date function to generate *unique* reference number;	
	
		$_SESSION['milkdudsrule'] = $refnumber; 

		header( "Location: http://www.sinbadisgettingold.com/page2.php" );
		}

?>
Then, on to page2.php:

Code: Select all

<?PHP 

		session_start(); 
		header("Cache-control: private"); 

?>
	
	some zippy html here. Your reference number is:
	
<?PHP
			 			  
			    if ( isset( $_SESSION['milkdudsrule'] ) ) {
					echo "$refnumber";
				}
				else {
					echo "could not start session";
				}
				
?>
On both pages, there is nothing before the opening php tags.
I've also tried removing the header() on page1.php, and applying the

Code: Select all

if ( isset( $_SESSION['milkdudsrule'] ) ) {
					echo "$refnumber";
				}
				else {
					echo "could not start session";
				}
code just to test it out, and it worked fine on page one.

i just can't seem to get the variable to pass on to page 2. Thanks in advance. Mondays are fun.

Posted: Mon Jan 23, 2006 6:02 pm
by feyd
cookies that are set in a page that redirects (in the header) are often ignored, as the browser sees "I gotta redirect!" first. Use an intermediate page, such as a "standard" page redirection.

Posted: Tue Jan 24, 2006 1:03 pm
by imstupid
thanks-
yeah, I'm too much of a fan of the copy + paste from old scripts method, but now it works fine.
i've updated the system from FORM PAGE > SUBMIT PAGE > THANK YOU PAGE to just FORM > SUBMIT and then displayed the "thanks for registering" crap on the SUBMIT PAGE. if that makes sense. Regardless, it works this way, and in one less page, so thanks again.

Posted: Tue Jan 24, 2006 5:16 pm
by Chris Corbyn
imstupid wrote:thanks-
yeah, I'm too much of a fan of the copy + paste from old scripts method, but now it works fine.
i've updated the system from FORM PAGE > SUBMIT PAGE > THANK YOU PAGE to just FORM > SUBMIT and then displayed the "thanks for registering" crap on the SUBMIT PAGE. if that makes sense. Regardless, it works this way, and in one less page, so thanks again.
I'm not sure I've understood you correctly but if I have it sounds as if you've left the browser in a situation where hitting F5 will re-submit your form data casuing duplicates. It's usually a good idea to use an intermediate page :) Glad you got it working either way :D