0

I'm having a really weird issue saving form values submitted through $_POST as session variables.

OK, so I have a form on page 1 which submits to page 2.

On page 2 I am setting all of the Post variables to Session variables.

On page 3 I am retrieving those variables.

On page 2 if I write something like this:

$_SESSION['fname'] = 'john';

It works fine. I can retrieve it on page 3.

If I write something like

$_SESSION['fname'] = $_POST['fname'];

That session variable is blank on page 3.

Even weirder, if I do something like:

$_SESSION['fname'] = $_POST['fname'].' Doe';

On page 3 I see just Doe.

Session variables that were previously set by other pages are also fine on page 3.

I have verified that the post variables are set on page 2, and I'm at a loss at this point. I'm not a PHP session expert or anything, but I have worked with them before and haven't seen anything like this.

Anyone have any thoughts?

Edit: The variable is being set in the session, it's just blank on page 3.

2
  • 1
    I would venture a guess that you "verified" the post variables incorrectly, and that they're misspelled or something to that effect.
    – zzzzBov
    Mar 29, 2011 at 18:46
  • Additionally I just verified that the session.save_path, session.cookie_path, session.name, and session_id() are the same across all 3 pages so there isn't any crazy multiple session issue going on that I can see.
    – Dan
    Mar 29, 2011 at 19:09

3 Answers 3

0

that means that something in the post ($_POST['name']) is not set;

try var_dump($_POST); on page 2 too see what actually got posted

7
  • array(1) { ["fname"]=> string(3) "Dan" }
    – Dan
    Mar 29, 2011 at 18:52
  • @Dan there is ur answer. switch to $_POST['fname'] and you should be good ^_^
    – Naftali
    Mar 29, 2011 at 18:53
  • @Dan then select the best answer for your question. if u do that more people would be more likeley tp phelp you out on future questions :-)
    – Naftali
    Mar 29, 2011 at 18:54
  • Sorry Neal, I was generalizing in my first post. I have about 60 variables, name was just an example. I am using fname for that particular variable when setting the session variable. I apologize, I should have used actual code in my original question.
    – Dan
    Mar 29, 2011 at 18:56
  • @Dan yes, yes you should use real code or else people would be debugging something that might not help at all ^_^
    – Naftali
    Mar 29, 2011 at 18:57
0

I can't resist asking: have you checked that method="POST" and not GET on the page 1 form?

-1

I believe the issue is because the POST data is not available when the SESSION data is written.

You can probably fix it by opening your php.ini and making sure that POST is registered before SESSION, this way, POST is available when you try to write SESSION data.

1
  • The superglobals are populated before your script gains control. The session is written out when the script ends, or you call session_write_close(). You don't have to 'register' POST. It's always there, and if the script was invoked via POST call, will have the POST's key/value pairs in it.
    – Marc B
    Mar 29, 2011 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.