sessions not working with header

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

sessions not working with header

Post by shiznatix »

Yes I posted this at the end of my last post but I think this deserves its own thread since its entirly different subject. I have this quick code snip:

Code: Select all

<?php

if (!empty($_GET['lang']))
{
    $langs = array('eng', 'est', 'fin', 'rus');
    
    $language = strtolower($_GET['lang']);
    $language = trim($language);
    
    if (in_array($language, $langs))
    {
        $_SESSION['lang'] = $language;
        
        header("Location: ".$_SESSION['last_page']);
        die();
    }
}

if (!empty($_SERVER['HTTP_REFERER']))
    $_SESSION['last_page'] = $_SERVER['HTTP_REFERER'];
else
    $_SESSION['last_page'] = $GLOBALS['sroot'];
?>
now when I header it the new value for $_SESSION['lang'] does not stay but if i don't header to the new location then it works fine (but i need the header!). Any ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

the documentation for session_write_close() explains it better than I can :wink:

basically, call session_write_close() before you send headers
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

if (!empty($_GET['lang']))
{
    $langs = array('eng', 'est', 'fin', 'rus');
    
    $language = strtolower($_GET['lang']);
    $language = trim($language);
    
    if (in_array($language, $langs))
    {
        $_SESSION['lang'] = $language;

        session_write_close();
        
        header("Location: ".$_SESSION['last_page']);
        die();
    }
}
does not work :cry:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$last_page = $_SESSION['last_page'];
        session_write_close();
       
        header("Location: ".$last_page);
shot in the dark, I havn't had experience with this function really..
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

no luck. anyone else? version info:

PHP Version 5.0.5-2ubuntu1.2
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Start the session prior to this redirection page being hit.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

feyd wrote:Start the session prior to this redirection page being hit.
say again? the session has already been started. this is confirmed when I put another session start above the setting of the new session[lang] as it gives me the error stuff. or did you mean somthing else?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I mean start the session on a previous page. There's no need to write out session headers (overall) after they have already been started previously.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

...i am? are you telling me to do somthing different? sessions have been started ahhhh!
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Two questions:

1. Did you use session_start() at all?
2 Which firewall do you have?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

yes, the session has already been started. this page is included in my header file that has my session start.

no, i don't have a firewall setup at all. no firewall.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

Can we see more of the script! Where is this being set, $_SESSION['last_page']. Also you say it not working, what exactly is happening?


printf!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

wait. never mind. forget i exist. please delete this thread.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

shiznatix wrote:no, i don't have a firewall setup at all. no firewall.
You really don't have a firewall?!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Oren wrote:
shiznatix wrote:no, i don't have a firewall setup at all. no firewall.
You really don't have a firewall?!
i like to live life on the edge.
Post Reply