Jump to content

php variables not being passed using $_SESSION


MFA

Recommended Posts

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!!

 

 

?>

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 4 weeks later...

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.php
PHP 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 2
PHP Warning:  Unknown: open(/var/php_sessions/sess_8dc57575562c346dcdf093266d7a46e4, O_RDWR) failed: No such file or directory (2) in Unknown on line 0
PHP 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).
 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.