Jump to content

Sessions And Session Variables


creacon

Recommended Posts

I have two consecutive apges (i.e. 1 loads 2) where I'm trying to pass information via a session variable, but when form 2 fetches the session variable, it's empty.Here's the page 1 code:

<?php	// If no session, start one	if (!isset($_SESSION)) 	{	  session_start();	}		// When the submit button is pressed,	if (isset($_POST['submit']))		{    			// gather the entered form data                                $fstname  = $_POST["fstname"];		$midinit  = $_POST["midinitial"];		$lastname = $_POST["lastname"];		$dob	  = $_POST["dob"];		$ssn	  = $_POST["ssn"];		$staddr1  = $_POST["staddr1"];		$staddr2  = $_POST["staddr2"];		$city	  = $_POST["city"];		$state	  = $_POST["state"];		$zip	  = $_POST["zip"];		$phone	  = $_POST["phone"];		// Set up the Full Name string                                $milen = (strlen($_POST['midinitial']));		$fnlen = (strlen($_POST['firstname']));		if ($fnlen == 1)			{$fstname = $fstname.".";}		if ($milen == 0)			{$fullname = $fstname." ".$lastname;}		else			{$fullname = $fstname." ".$midinit.". ".$lastname;}					// Combine the two address fields		$staddr = $staddr1." ".$staddr2;		$ctstzp = $city.", ".$state." ".$zip;		                                // Create & Assign a session variable		$_SESSION['$strname'] = $fullname;                                // Then display if for debugging purposes		echo "Fullname = |".$_SESSION['$strname']."|";				// Finally, load the questionnaire form after a 10 second delay for debugging		echo '<META http-equiv="refresh" content="10;URL=WOTCPg2.php">';	//		header('Location: WOTCPg2.php');		}?><html>  <head> 

and here's the <form> tag that executes it (just in case you need to see it).

        <form name="QstnrPg0" method="post" onSubmit="return show_message();" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Here's the code at the top of page two:

   <?php                // When the Submit button is pressed, go to the log off page	if (isset($_POST['submit'])){		header("Location: WOTCLogoff.php");}                // Otherwise set upt the information for the page heading	else{    			// retrieve the session variable from page 1			$fullname =$_SESSION['$strname'];                                                               // and echo its contents for debugging purposes		echo "Session Variable =  |".$_SESSION['$strname;']."|";}	?> <html>  <head>

The first page displays the session variable exactly as assigned, but on the second page the retrieved session variable is empty.When I run the pages on my local host (i.e. xampp) I get the following warning messages as soon as the page opens.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\IOD\WOTCPg1.php:5) in C:\xampp\htdocs\IOD\WOTCPg1.php on line 10Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\IOD\WOTCPg1.php:5) in C:\xampp\htdocs\IOD\WOTCPg1.php on line 10

They DO NOT display when I run the pages on my remote server. What do those warnings mean???? What am I missing or doing wrong????

Link to comment
Share on other sites

First, you should always be using session_start. Don't check if $_SESSION is set, it's always set. Always use session_start regardless. The session_start function isn't only for creating a new session, it also starts the user's existing session.For the other warnings, check this thread:http://w3schools.invisionzone.com/index.ph...ost&p=96607

Link to comment
Share on other sites

First, you should always be using session_start. Don't check if $_SESSION is set, it's always set. Always use session_start regardless. The session_start function isn't only for creating a new session, it also starts the user's existing session.For the other warnings, check this thread:http://w3schools.invisionzone.com/index.ph...ost&p=96607
Thanks for your feedback and the link. That fixed everythin up fine. I did the rearrangement, and removed the if from the session_start, and now everything's working.Thanks again.
Link to comment
Share on other sites

First, you should always be using session_start. Don't check if $_SESSION is set, it's always set. Always use session_start regardless. The session_start function isn't only for creating a new session, it also starts the user's existing session.For the other warnings, check this thread:http://w3schools.invisionzone.com/index.ph...ost&p=96607
Now that I got that working, I tried to change the code to what I really wanted to do. In page 1 I have:
		// Start or Continue a session			session_start();		// When the submit button is pressed,	if (isset($_POST['submit']))		{    			// gather the entered form data                                $fstname  = $_POST["fstname"];		$midinit  = $_POST["midinitial"];		$lastname = $_POST["lastname"];		$dob	  = $_POST["dob"];		$ssn	  = $_POST["ssn"];		$staddr1  = $_POST["staddr1"];		$staddr2  = $_POST["staddr2"];		$city	  = $_POST["city"];		$state	  = $_POST["state"];		$zip	  = $_POST["zip"];		$phone	  = $_POST["phone"];		// Set up the Full Name string                                $milen = (strlen($_POST['midinitial']));		$fnlen = (strlen($_POST['firstname']));		if ($fnlen == 1)			{$fstname = $fstname.".";}		if ($milen == 0)			{$fullname = $fstname." ".$lastname;}		else			{$fullname = $fstname." ".$midinit.". ".$lastname;}					// Combine the two address fields		$staddr = $staddr1." ".$staddr2;		$ctstzp = $city.", ".$state." ".$zip;				// Generate an array from the above data		$strappl = $fullname."\",\"".$dob."\",\"".$ssn."\",\"".$staddr."\",\"".$ctstzp."\",\"".$phone;		$arrappl = Explode(",",$strappl );		// Then declare a session variable &		// assign the form data array to it		$_SESSION['$sesarrappl'] = $arrappl;				// Finally, load the questionnaire form		echo '<META http-equiv="refresh" content="10;URL=WOTCPg2.php">'; // 10 seconds for degug	//		header('Location: WOTCPg2.php');		}?>

Then in the second page, I have:

  <?php		// Continue the session	session_start();	if (isset($_POST['submit'])){//		header("Location: [url="https://www.8f7.com/4DACTION/WebShowMenu&quot%3b%29;"]https://www.8f7.com/4DACTION/WebShowMenu");[/url]		header("Location: WOTCLogoff.php");}	else{    			$applarr  = $_SESSION['$sesarrappl'];		$fullname = $applarr[0].", DOB".$applarr[1];}	?> 

And finally in the <form tag I have:

        <input name="fullname" type="text" id="fullname" value="<?php echo $fullname; ?>" size="45" maxlength="45">

The problem is that the value= ONLY prints the first element of the variable. The field length is 45, and the data are much less. If, in the php code I set $fullname = " DOB", the text field will display that, but if I do any concatenation at all, ONLY the first element displays. Can anyone tell me what causes this? Previously, when I was saving the data to a file in page 1, then reading the file in page 2, it worked correctly (e.g. it displayed "Dudley D. Doright, DOB 12/31/1966").

Link to comment
Share on other sites

First, it's probably better not to use $ in array keys, that's going to cause a problem if you use double quotes with the key. You can just leave that out, e.g.:$_SESSION['sesarrappl'] = $arrappl;Also, instead of doing this:

$strappl = $fullname."\",\"".$dob."\",\"".$ssn."\",\"".$staddr."\",\"".$ctstzp."\",\"".$phone;$arrappl = Explode(",",$strappl );

you can just do this:

$arrappl = array($fullname, $dob, $ssn, $staddr, $ctstzp, $phone);

or, if you want to use names for keys:

$arrappl = array(  'fullname' => $fullname,   'dob' => $dob,   'ssn' => $ssn,   'staddr' => $staddr,   'ctstzp' => $ctstzp,   'phone' => $phone);

On the second page, after you get the array from the session use print_r to see what's in it:print_r($applarr);

Link to comment
Share on other sites

VOILA! That did the trick. Now it works. I swear, I don't know where I'd be if it weren't for all you guys. I can't thank you enough; you've been so helpful to a newbie dinosaur like me. For all the world, after 50 years of programming, when I look at this web programming I feel like I've never even seen a computer before, let alone program one. When I learned Paradox, I thought that it had to be the most complex languange on the planet, but boy was I wrong.Anyway, I'm going to learn this if it kills me (and at 77, maybe that's a good thing), so thanks again for all your help, and see you at my next problem. (LOL).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...