MFA Posted February 17, 2013 Share Posted February 17, 2013 my variables from page 1 are not being sent to page 2 using sessions. i have tried to echo them and it is not working. when i use print_r for the sessions on page2.php, all i see is "array()" on my site. can someone please help. page1.php <?php session_start(); { $_SESSION['questionsession'] = $question; $_SESSION['asession'] = $a; $_SESSION['bsession'] = $b; $_SESSION['csession'] = $c; $_SESSION['dsession'] = $d; $_SESSION['esession'] = $e; $_SESSION['answersession'] = $answer; $_SESSION['correctsession'] = $correct; } ?> page2.php <?php session_start(); $question = $_SESSION['questionsession']; $a = $_SESSION['asession']; $b = $_SESSION['bsession']; $c = $_SESSION['csession']; $d = $_SESSION['dsession']; $e = $_SESSION['esession']; $answer = $_SESSION['answersession']; $correct = $_SESSION['correctsession']; echo $question; // NOT WORKING!! ?> Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/ Share on other sites More sharing options...
Jessica Posted February 17, 2013 Share Posted February 17, 2013 What's with the random { }? Did you verify those variables are set on page 1? Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1412856 Share on other sites More sharing options...
MFA Posted February 17, 2013 Author Share Posted February 17, 2013 yes those variables work and are obtained from a mysqli_query. I used the {} just to group them together lol Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1412860 Share on other sites More sharing options...
Jessica Posted February 17, 2013 Share Posted February 17, 2013 That's weird. I'd expect that to produce a warning, but ok. It's probably something to do with the rest of your code, but we can't tell if you don't post it. try adding print_r($_SESSION); after defining them. You're also doing a LOT of unnecessary typing the way you have it. You could probably just put the row you get from MySQL directly into the session. Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1412862 Share on other sites More sharing options...
MFA Posted February 17, 2013 Author Share Posted February 17, 2013 did it and got "Array ( [questionsession] => Which of the following is a side effect of furesomide? [asession] => Sample answer A [bsession] => Sample answer B [csession] => Sample answer C [dsession] => Sample answer D [esession] => Sample answer D [answersession] => The answer is [insert explanation]. If you see this text, SYSTEM works. [correctsession] => B )" so it seems to work. It's probably something to do with the rest of your code, but we can't tell if you don't post it. try adding print_r($_SESSION); after defining them. mcq.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?php session_start(); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php include 'dbyear2.php'; $qnumber = ($_REQUEST['uqn']); // obtain question number from URL $find = mysqli_query($condbyear2, "SELECT * FROM Renal WHERE UQN='$qnumber'"); $retrieve=mysqli_fetch_assoc($find); { $question = $retrieve['question']; $a = $retrieve['MCQ_A']; $b = $retrieve['MCQ_B']; $c = $retrieve['MCQ_C']; $d = $retrieve['MCQ_D']; $e = $retrieve['MCQ_E']; $answer = $retrieve['answer']; $correct = $retrieve['MCQ_correct']; } { $_SESSION['questionsession'] = $question; $_SESSION['asession'] = $a; $_SESSION['bsession'] = $b; $_SESSION['csession'] = $c; $_SESSION['dsession'] = $d; $_SESSION['esession'] = $e; $_SESSION['answersession'] = $answer; $_SESSION['correctsession'] = $correct; } ?> <form action='check.php' method='POST'> <table width="90%" border="0%" cellpadding="6" align="center"> <tr><td width="6%"></td><td width="94%"><?php echo $question; ?></td></tr> <tr></tr> <tr><td><input type='radio' name='group1' value='A' /></td><td> <?php echo $a; ?></td></tr> <tr><td><input type='radio' name='group1' value='B' /></td><td> <?php echo $b; ?></td></tr> <tr><td><input type='radio' name='group1' value='C' /></td><td> <?php echo $c; ?></td></tr> <tr><td><input type='radio' name='group1' value='D' /></td><td> <?php echo $d; ?></td></tr> <tr><td><input type='radio' name='group1' value='E' /></td><td> <?php echo $e; ?></td></tr> <tr></tr> <tr><td><input type="submit" value="Submit"></td></tr> </table> </form> </body> </html> check.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?php session_start(); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php include 'dbyear2.php'; $question = $_SESSION['questionsession']; $a = $_SESSION['asession']; $b = $_SESSION['bsession']; $c = $_SESSION['csession']; $d = $_SESSION['dsession']; $e = $_SESSION['esession']; $answer = $_SESSION['answersession']; $correct = $_SESSION['correctsession']; // must insert table here again $choice = $_POST['group1']; echo $question; if (!isset($choice)) { echo "You did NOT select an answer. The correct choice was $correct"; } else if ($choice == $correct) { echo "Well done - You chose the correct answer $correct"; } else {echo "Incorrect answer! - The correct choice was $correct"; } echo "$answer"; // Copy the detailed answer here // end session? and destroy the set varaibles? - as this is 1MCQ answer mode not TEST mode. ?> </body> </html> You're also doing a LOT of unnecessary typing the way you have it. You could probably just put the row you get from MySQL directly into the session. yeah, true but i'm using the variables in my form so its quicker to type then session[''] each time. but i suppose it would be the same if i just did what you said cause i wouldnt have to waste time assigning variables. thank you. Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1412866 Share on other sites More sharing options...
Jessica Posted February 17, 2013 Share Posted February 17, 2013 session_start() needs to be the VERY first line. You can't start a session after outputting HTML. Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1412869 Share on other sites More sharing options...
MFA Posted February 17, 2013 Author Share Posted February 17, 2013 I didn't know <!doctype> constituted as HTML. anyway, i put the session_start() above doctype so its the first line in both files however it still doesnt work. i did print_r($_SESSION) and got Array ( ) Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1412872 Share on other sites More sharing options...
MFA Posted February 20, 2013 Author Share Posted February 20, 2013 bump.. anyone have any ideas? - i have been trying for days and it still doesnt work. Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1413691 Share on other sites More sharing options...
MFA Posted March 17, 2013 Author Share Posted March 17, 2013 So, I finally fixed it. And for anyone else who has/will suffer(ed) from a similar problem, the solution was to reset your CGI error log. Mine was saying: 20130316T222720: www.webaddress.com/webpage.phpPHP Warning: session_start(): open(/var/php_sessions/sess_8dc57575562c346dcdf093266d7a46e4, O_RDWR) failed: No such file or directory (2) in /hermes/waloraweb077/b1980/webaddress.com/webpage.php on line 2PHP Warning: Unknown: open(/var/php_sessions/sess_8dc57575562c346dcdf093266d7a46e4, O_RDWR) failed: No such file or directory (2) in Unknown on line 0PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_s And yes, the session.save_path was in the correct place (not sure why it was saying that). Quote Link to comment https://forums.phpfreaks.com/topic/274580-php-variables-not-being-passed-using-_session/#findComment-1419110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.