Session variable is disappearing before browser closes

Discussion in 'PHP' started by bvfrezz, May 21, 2009.

  1. #1
    I have a quick question about session variables. Below is the code I am using to create the session ID.

    <?php
    session_start();
        if($_REQUEST['id']){
            $_SESSION['id'] = $_REQUEST['id'];
            $host  = $_SERVER['HTTP_HOST'];
            $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            $hidden = "http://" . $host . $uri;
            //header("Location: " . $hidden);
        }
    
        $id = $_SESSION['id'];
    ?>
    Code (markup):
    I call the ID in a text field when the page loads. "value='<?php echo $_REQUEST['id'];?>"

    Now, if a visitor navigates away from this page, the value disappears if they return to the page. Is there something I am doing wrong here? I'm assuming it's something simple, but I can't find the issue. I just want the session ID to be stored until the window closes so I can retrieve it in the text field.

    Thanks in advance for your help.
     
    bvfrezz, May 21, 2009 IP
  2. OmegaMan

    OmegaMan Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you have session_start() in the top of every page? If you have, then I can't tell what's wrong..
     
    OmegaMan, May 21, 2009 IP
  3. bvfrezz

    bvfrezz Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, I have it in the header. It is passed from a PHP configuration file to the header. Could that be the issue? I am going to try and work through the issue and come up with a solution. I will post it if I find one. Thanks.
     
    bvfrezz, May 21, 2009 IP
  4. catgnome

    catgnome Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Add this line at the top of all your PHP code :
    <?php
    $session_length = 60;   // 60 minutes
    session_cache_expire($session_length);
    ?>
    PHP:
     
    catgnome, May 21, 2009 IP
  5. websea

    websea Peon

    Messages:
    572
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try session_register() function or make sure your are getting value for $_REQUEST['id']
     
    websea, May 22, 2009 IP
  6. bvfrezz

    bvfrezz Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think this may have done the trick. I used the original code and simply added the session_length code to the top. Thank you.
     
    bvfrezz, May 23, 2009 IP