Reputation: 45
i found many answers about that problem but nothing solved my problem - so i want to show you my code and hope that someone can find the mistake..
I have a standard HTML formular that gives some data with POST to the next .php file where i get it and save it into session-variables. I use the session variables about 2 reasons:
Here is the code:
session_start(); // Handle Variables on post and reloaded-page if(isset($_POST["locId"]) && isset($_POST["dateId"]) ) { $locId = htmlspecialchars($_POST["locId"]); $dateId = htmlspecialchars($_POST["dateId"]); $_SESSION["locId"] = $locId; $_SESSION["dateId"] = $dateId; echo "Session variables are set: locId = " . $_SESSION["locId"] . " dateId = " . $_SESSION["dateId"]; } elseif(isset($_SESSION["locId"]) && isset($_SESSION["dateId"])) { echo "get it from session"; $locId = $_SESSION["locId"]; $dateId = $_SESSIOn["dateId"]; } else { $load_error = 1; $status = "alert alert-danger"; $message = "shit, no variables here"; }
The frist call works fine - session variables are set and the echo gives the right values. After reloading the page i get the echo "get it from session" but my variables have no values.
i also checked my session_id() on first call and reload.. they are NOT the same.
I testet a simple test.php file where i start a session with a variable and ask for the variable in the next file. It works fine :-/
Its just a problem with my code above. I think my webserver is handling right. But what reasons are there for chaging a session id and losing session-variable values?
Upvotes: 0
Views: 2084
Reputation: 45
Damn! To write correct is everything ...
I found my mistake.
Look at the code in my question. The second session-variable is $_SESSIOn["dateId"].. the n is lowercase! If i write it correctly and complete in UPPERCASE it is working.
Also the session_id is not chaging anymore and i can output the session_id() as much as is want.. but one mistake in $_SESSIOn changes everything. New session_id on every call, ... strange.
Learned something again :-) Thanks to everybody for the answers and your time! I hope i can help you in the future
Upvotes: 2
Reputation: 1987
Well, your mistake is quite easy to find. In fact, your code works perfectly. But look at this part:
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
Well, you asign the session values to two variables, but in fact, you simply missed to output them anywhere. Thats why you get "get it from session" but then is displays nothing, you need to echo
them.
Simply add an echo
and it will display your vars perfectly :)
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
echo $locId;
echo $dateId;
Upvotes: 0