$_SESSION variables are being trashed...

Discussion in 'PHP' started by gil_e, Dec 17, 2007.

  1. #1
    In my app, the user selects a state from a map. Selecting that state takes you to the first PHP page with the request var of "state" being passed in. I set the $_SESSION['state'] = "georgia" (for instance), then every page from that point forward, uses the state as part of the SQL query to focus info to just that state.

    The issue I have is this. You can visit the first page, and all the counties in the given state show up as selectable. If you select the form again, it works fine, but at a point (typically3 or 4 times) the $_SESSION['state'] returns an empty value causing the SQL statement to be invalid.

    What is happening to my session value? I have searched the code and no-where am I invalidating that session variable other than when the user exits the site. I have put echo(...) comments to help, but nothing is showing up.

    Can anyone help me here?
     
    gil_e, Dec 17, 2007 IP
  2. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #2
    please post your php code here i will check it
     
    sunnyverma1984, Dec 17, 2007 IP
  3. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Do you put session_start(); at the top of each page ? Try putting print_r( $_SESSION ); at the top of each page so that you can track down which page your variables are getting trashed.

    Brew
     
    Brewster, Dec 17, 2007 IP
  4. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah Brewster has probably got the right answer there!

    If you don't use session_start() at the top of each script you wont be able to access the session variables.
     
    tonybogs, Dec 17, 2007 IP
  5. gil_e

    gil_e Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sunnyverma,

    I have simplified the code, removed any included and mainlined them into the example and am still getting the same results. Initially, when the page comes up, you get the county name, but after you refresh a few times (not consistent), it eventually will be an empty array(). Also, the $_SESSION value is not coming up at all in IE, but works as explained in Firefox.

    Here is the simplified code:


    <?php session_start(); ?> 
    
    <html> 
    <head> 
      <title>Test Database</title> 
      <link rel="stylesheet" href="../styles/style.css" type="text/css"> 
    </head> 
    
    <body bgcolor="#F2F4C4"> 
      <h1><center>Sample Database</center></h1> 
      <font face="Arial"> 
      <center> 
        <hr width="80%"/> 
    
        <br> 
        <a href="./advanced_search.php">Search by County</a> 
    
    <? 
        print_r($_SESSION);     //If you refresh the browser page, this will eventually return array(). 
    ?> 
    
        <hr width="80%"/> 
        <h5>Developed by <a href="http://www.bgesoftware.com" target="_blank">BGE Software, Inc.</a></h5> 
      </center> 
      
      </font> 
    </body> 
    </html> 
    PHP:
     
    gil_e, Dec 18, 2007 IP
  6. gil_e

    gil_e Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes I am using session_start() at the top.
     
    gil_e, Dec 18, 2007 IP
  7. gil_e

    gil_e Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am using session_start() at the top of the script.
     
    gil_e, Dec 18, 2007 IP
  8. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #8
    i think it is not a coding problem. But it is due to server configration edit session.gc_maxlifetime in php.ini
    increase time. I hope it will work
     
    sunnyverma1984, Dec 19, 2007 IP
  9. rtrocc

    rtrocc Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yea, i was thinking that the session is being reset either by timeout or a problem on the server. It has happened to me before. Look at the php.net reference for sessions... you should find some valuable info that can solve your problem. sessions dont live forever, unless you set them to and even then are not consistent because they are always going to be temporary.
     
    rtrocc, Dec 19, 2007 IP