Session resetting on page reload

Discussion in 'PHP' started by greenlizard, Jun 27, 2010.

  1. #1
    I have a search page that isn't saving the session. The form shows the search results on the same page. I can submit the form and the search results show. If I leave the page and come back, or reload the page, the form (which is drop down boxes) resets and it acts like you never searched for anything.

    I have session_start(); at the beginning which I know can be a common problem so that's not it.

    I want to believe that it has to do with the drop down boxes believe it or not. I say this because I have another search page set up the same way but it uses text boxes instead of drop downs, and it saves the session data just fine. There's something with the drop down box form that resets everything when the page reloads.

    Any ideas?
     
    Last edited: Jun 27, 2010
    greenlizard, Jun 27, 2010 IP
  2. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    if (!isset($_SESSION)) {
    	session_start();
    }
    PHP:
    sounds like the session is being restarted and loosing the value
     
    themullet, Jun 27, 2010 IP
  3. greenlizard

    greenlizard Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Hmm that didn't resolve it. Since that didn't work I think my session just isn't being stored at all. The search results for the 1st page show just because it has the data fresh from the form. But when I go to page 2, I get no results even when adding the code above. So, I'm concluding that for some reason, my session variables aren't being stored. Sigh.
     
    Last edited: Jun 28, 2010
    greenlizard, Jun 28, 2010 IP
  4. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    if (isset($_SESSION)) {
    foreach ($_SESSION as $v => $a) {
    		echo "$v - $a<br/>";
    	}
    }
    PHP:
    also looking at your code session year, month and day don't get set
     
    themullet, Jun 28, 2010 IP
  5. greenlizard

    greenlizard Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5

    I love you! Thank you so much!! I really have been working on this for 3 days straight. All this time I wasn't setting the date for the session. :rolleyes:
     
    greenlizard, Jun 28, 2010 IP
  6. greenlizard

    greenlizard Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK, I thought it was fixed. But I guess not. When I click the next button and go to the next page, the session data is echoed at the top but, it still doesn't show the next results.

    It shows this at the top, so I know the session data is there. But again, I go to the next page and it doesn't work.
    year - 2010
    month - 07
    day - 10
    action - godate
     
    Last edited: Jun 28, 2010
    greenlizard, Jun 28, 2010 IP
  7. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    looks like you are over writing the session value with an empty post value. This should fix it.

    if (isset($_POST[year]) {
    $_SESSION[year]=$_POST[year];
    }
    if (isset($_POST[year]) {
    $_SESSION[month]=$_POST[month];
    }
    if (isset($_POST[year]) {
    $_SESSION[day]=$_POST[day];
    }
    PHP:
     
    themullet, Jun 28, 2010 IP
  8. greenlizard

    greenlizard Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    When I copy and paste that code you provided I get an error and the page doesn't even load. Is this code correct? No matter where in the page I place it, it causes the page not to load.

    
    if (isset($_POST[year]) {
    $_SESSION[year]=$_POST[year];
    }
    if (isset($_POST[year]) {
    $_SESSION[month]=$_POST[month];
    }
    if (isset($_POST[year]) {
    $_SESSION[day]=$_POST[day];
    }
    
    PHP:

    Wow, who knew sessions would be so difficult?

    Maybe I need to stop the form from submitting or something? I'm totally confusing myself now.
     
    Last edited: Jun 28, 2010
    greenlizard, Jun 28, 2010 IP
  9. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #9
    sorry my bad, didn't close my brackets properly

    if (isset($_POST[year])) {
    $_SESSION[year]=$_POST[year];
    }
    if (isset($_POST[year])) {
    $_SESSION[month]=$_POST[month];
    }
    if (isset($_POST[year])) {
    $_SESSION[day]=$_POST[day];
    }
    PHP:
    weird not working in ff, in theory php is the server side producer so should kick out the code no matter what the browser
     
    themullet, Jun 28, 2010 IP
  10. greenlizard

    greenlizard Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10

    OK, now it's working in ff and other browsers.

    I think that fixed it.

    Phew... php is so sensitive! :)
     
    greenlizard, Jun 28, 2010 IP
  11. greenlizard

    greenlizard Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    One quick question, it is working but, why do you repeat the post[year] part three times in your code above instead of doing year, then month, then day?
     
    greenlizard, Jun 28, 2010 IP
  12. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #12
    Sorry bad copy and paste to update, should be:
    
    if (isset($_POST[year])) {
    $_SESSION[year]=$_POST[year];
    }
    if (isset($_POST[month])) {
    $_SESSION[month]=$_POST[month];
    }
    if (isset($_POST[day])) {
    $_SESSION[day]=$_POST[day];
    }
    PHP:
     
    themullet, Jun 28, 2010 IP