开发者

Strange PHP bug with sessions

I'm trying to load a $_SESSION variable with another variable on a page of mine.

The page structure here is Page 1 -> Page 2 -> Page 3; it is a multi-part form. There is a back button on Page 2 that allows you to go back to Page 1. I'm trying to use session variables to keep the input fields in Page 1 from going blank.

I use a form submit to go from Page 1 to Page 2. Here is the applicable PHP code on Page 2 that handles retrieving POST variables:

    <?php
$retailerName = $_POST["retailerName"];
$description = $_POST["description"];
$savingsDetails = $_POST["savingsDetails"];
$terms = $_POST["terms"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$zone = $_POST["zone"];
$dateExp = $_POST["dateExp"];
$tag = $_POST["tag"];

$_SESSION["rn"] = $retailerName;
$_SESSION["de"] = $description;
$_SESSION["sd"] = $savingsDetails;
$_SESSION["tm"] = $terms;
$_SESSION["ph"] = $phone;
$_SESSION["ad"] = $address;
$_SESSION["zo"] = $zone;
$_SESSION["ex"] = $dateExp;
$_SESSION["tg"] = $tag;
?>

I know that the variables are being set properly.

I can do this:

 echo $retailerName;
 echo $_SESSION["rn"];

On Page 2 and it will show what I want. The only problem is that the session variables won't carry over to Page 1.

If I set the session variables to a string, it will carry back to Page 1. For some reason, even though the posted variables are valid and contain the proper string, the session variable won't carry them over.

If I do this:

 $_SESSION["rn"] = $retailerName;

Then the session variable's value will not carry over to Page 1.

If I do this:

 $_SESSION["rn"] = "asdf";

Then the session variable's value will carry over to Page 1.

Normally, this just means my posted variables are bad, but in this case I know that they work, since the rest of the page is dedicated to outputting the posted variables, and it does so correctly.

The SESSID stays the same, so that's not the problem. I also am able to carry over other session variables in other parts of the 开发者_StackOverflowwebsite, and they work perfectly.

Why is it that the $_SESSION variables will not carry over a proper and valid posted variable to Page 1, but it will carry over a string?

Page 1 Pastebin

Page 2 Pastebin


Did you forget

session_start();

At (or near) the top of your PHP?

Is the form's type attribute set to POST?


What happens if you do var_dump($_SESSION) of print_r($_SESSION) when going back to page 1? Seems like you did use session_start() because $_SESSION['rn'] = "abc" does work.

You should also be aware that the settings on top of the file (so $retailerName = $_POST['retailer']) should be within some checks fe.

if($_SERVER['REQUEST_METHOD'] == "POST") {
  $retailerName = $_POST['retailer'];
  $_SESSION['rn'] = $retailerName;
}

Otherwise everything will be wiped away when visiting the page again (and thus having an empty $_POST array which erases the $_SESSION['rn'] value.


Not sure of the complete scenario here but.....

Seems to me there is an issue with the flow of events here, I would try including the statements to set the session variables in an if statement like:

if($retailerName){
  $_SESSION['rn'] = $retailerName;
  ...........
}

Maybe somehow there is another call to page 2 AFTER setting the POST vars, which resets the session vars to empty cause there is no POST data.

Also by any chance these page 1 and page 2 are being used in iframes? If so then maybe the sequence of loading page 1 and page 2 isn't right.


Do you have that same code at the top of your page 1?

$retailerName = $_POST["retailerName"];
$description = $_POST["description"];
$savingsDetails = $_POST["savingsDetails"];
$terms = $_POST["terms"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$zone = $_POST["zone"];
$dateExp = $_POST["dateExp"];
$tag = $_POST["tag"];

Maybe all your variables are being reset before you try to get the properties with the following:

$_SESSION["rn"] = $retailerName;
echo $_SESSION["rn"] // this will not be what you want now

The only thing I can think is that whatever is on page 1 is resetting the $_SESSION array. At the top of page 1 and page 2 do a print_r($_SESSION) and do a print_r($_GET) at the top of page 2 and another print_r($_SESSION) at the bottom of page 2.

Now go to page 1 in your browser. Are your values set to null there? Fill out your form on page 1 again and post to page 2. Does page 2 show your form values? Maybe the form values aren't being passed to page 2 correctly.

If you see your form values, then your setting of properties in the $_SESSION array should work. You should see the $_SESSION array properly printed out at the bottom of page 2.

Go back to page 1. Are the sessions variables printed out? If not, then something is overwriting the $_SESSION array. Check included/required files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜