0
votes

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:

  1. if someone reloads the page, it should show the same information as before.
  2. I need the variables in upcoming php files.

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?

3
I've just tried on my machine and it works fine. - IsThisJavascript
@WillParky93 With his code? Not possible it showed you something, he missed to echo the vars. - Twinfriends
He claims his variables have no value, which I'm assuming he's trying to access from below this snippet. I did echo the values and found that I could access them. So therefore, his code does work and the sessions are working correctly.. - IsThisJavascript
@WillParky93 Well thats true. That will work perfectly. But if you read in his question: After reloading the page i get the echo "get it from session" but my variables have no values. - So I'm assuming he expected them to be outputed directly there, but he simply missed to echo them. Next time may add a little note that you added an echo for his variables and it work. Because as you wrote it above, I thought it worked with copy + paste, without have done any changes by you. - Twinfriends
Yes, you are right. In my code below there are some mySQL queries which uses the values of my variables to get some information and display it on the page. But if i echo my session-variables they are empty. - Niklas

3 Answers

2
votes

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

0
votes

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;
-2
votes

Try this:

session_id();

session_start();