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

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
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

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

Post 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.
Last edited by imstupid on Tue Jan 24, 2006 1:03 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
Post Reply