Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post

    Redirecting without destroying session.

    Hey,

    I have a small problem. I have a script where it'll take the link from the database and redirect it.
    PHP Code:
    <?php
    session_start
    ();
    require (
    'connect.php');

    if(!isset(
    $_SESSION['username'])){
        echo 
    'Please <a href="login.php">login</a> to access this area!';
    }
        
    if(isset(
    $_SESSION['username'])){    
    //Get Link Code
    }
    header"Location:".$_GET['target'] ) ;
    exit;
    ?>
    The trouble I have is exit; destroys the session but I need the session to still be there. Is there any way I can redirect without destroying the session?
    Last edited by Skippy; 06-24-2010 at 12:12 AM.

  • #2
    New Coder
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Try calling session_write_close(); before the header for the correct fix and it should work correctly. Alternatively: instead of doing a header to another page, you could have it read the contents of the specified website and keep the website read on the same page. You could also use frames. A header redirect normal stops execution anyways, so it can't be exit.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Oh, thats interesting xGIHavoc... I usually have to exit my header redirects to get it to work properly. Regardless, Is that all the code? I've never had a redirect destory a ession, but yah, I'd also recommend session_write_close().

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    2
    Thanked 3 Times in 3 Posts
    A slight misunderstanding, but partially. He said the trouble was from exit, I'm sorry for my bad phrasing ^_^. Certain browsers automatically stop loading information from a page once a header redirect is sent, others DO need an exit from what I am aware of.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yah, that makes sense.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I cannot say I've ever had a destruction problem due to a header call.

    Regardless of how a browser treats the header, once the script terminates a session will close. Exit or script end will flush the file. If a browser actually redirects before script end (I seem to recall hearing of this issue on some older handheld devices), the script will continue to process until it has been completed or exit is called. In the event that a session file is locked, it should stall the execution of the next attempt until it can be unlocked (until the max execution time is exceeded). Once unlocked, the next script can aquire a lock on it. It will not delete the session. This is why we force a session_write_close. Personally, I haven't had to force this since framesets went out of style.
    My immediate assumption is that the session is being passed via get through the PHPSESSID. In this case, the session is not terminated, rather it becomes irretreivable when the user reconnects to your site. You can verify this by simply looking at the address bar, if PHPSESSID exists, either session.use_trans_sid has been enabled or session_id has been added to your links. If you see this, check your cookies to see if a PHPSESSID cookie has been set. If not, that is your problem right there. This can be corrected if your redirecting to your own site by appending your sid on it, but cannot be corrected for offsite redirections. Another potential fault could be the use of session_regenerate_id being called and not able to either: lock the original file, or set a new session cookie. Your error reporting should be enabled to see if this is the case.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •