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 abl开发者_如何转开发e to set a $_SESSION variable to the value of the $_POST variable on Page 2, and 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). 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.
What I am trying to do: Put the value of a $_POST var from Page 2 into a $_SESSION var, and have it go back to Page 1.
My Problem: The session vars don't carry back to Page 1 from Page 2. They can carry a string, but not the POST variables. 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 not a 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 e.g. 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 e.g. 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 :)
精彩评论