简体   繁体   中英

Odd Problem with PHP $_SESSION

I'm having a problem carrying over a variable from Page 2 to Page 1. I have a multi-part form, and the idea is to be able to save your input in session variables if you decide to go back to Page 1 (which is the page with the fields.

Page 1 has a POST action to Page 2. Then, in Page 2, the $_SESSION variables are set from the POST variables. Keep in mind, the POST variables were set correctly, as I call them in a later part of Page 2 with success .

My problem is this: I am not able to set a $_SESSION variable to the value of the $_POST variable on Page 2, carry it over to Page 1. I can set and output a $_SESSION variable from the POST variable on Page 2 (it just doesn't carry back to Page 1). 其传递到页面1。我可以从页面上的POST变量设置和输出$ _SESSION变量。 2(只是不返回到第1页)。 I can set the $_SESSION var to a string, however, and have it carry over to Page 1. Normally, this would be evidence that the POST vars are bad, but they carry the correct values properly.

Put the value of a $_POST var from Page 2 into a $_SESSION var, and have it go back to Page 1. 将第2页中的$ _POST变量的值放入$ _SESSION变量中,然后返回到第1页。

The session vars don't carry back to Page 1 from Page 2. They can carry a string, but not the POST variables. 会话var不能从页面2携带回到页面1。它们可以携带字符串,但不能携带POST变量。 The POST variables are not the problem, as I described above, so please don't try to troubleshoot by checking POST var validity, etc. They output perfectly.

I can do a session dump on Page 2, and all of the session vars I need are filled properly.

If I go back to Page 1 after that, the session vars in question are set to NULL. Keep in mind, I have dozens of other session vars working on that particular page. It is a session_start(); session_start(); problem.

The zinger - I can carry a session variable over to Page 2 from Page 1, no problem.

Page 1 Pastebin

Page 2 Pastebin

the session vars in question are set to NULL

and

They can carry a string, but not the POST variables

Your code is unintentionally over writing the session variables. You have this code which is perfectly OK:

$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;

Now what happens if you open this page via GET request ? The string you saved in the session will carry over, the post variables you save in the session eg by doing a $_SESSION["tg"] = $_POST["tag"] will become NULL.

Now try this -- add a counter variable in your page2 that counts how many times this page was opened:

if(array_key_exists("ViewCount", $_SESSION)==false){
    $_SESSION["ViewCount"] = 0;
}
$_SESSION["ViewCount"]++;
echo $_SESSION["ViewCount"];

Tell me if the counter increments unexpectedly eg increments twice instead of once every time you POST to that page. Also use a net inspector to see if your browser makes a GET request to the same page after the POST (FireBug can help you verify; server logs will give you an even greater insight). If this is the case then you probably have an <img> tag in your page with src="" .

You can serialize the POST variables and put it on one session variable.

Same thing, you can json_encode the POST variables and put in in one session variable.

Both return a string which you said can be carried.

I recommend json_encode :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM