Re: Sessions and header-redirect

From: Date: Fri, 20 Apr 2001 23:27:26 +0000
Subject: Re: Sessions and header-redirect
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-49619@lists.php.net to get a copy of this message
Nope, you can use header() after starting a session. This works fine, even if a session is already started: session_start(); header("Location: http://www.fbi.gov"); Creapy, but it works. You see, when a "header" is sent, it's not actually "sent". PHP collects all headers and fills out a header table, which it sends to the webserver once the script ends or when the first piece of data is sent to the browser (whichever comes first). That's why you can make 10 calls to header and all of them will work. If you are getting the "headers already sent error" it's most likely because you unknowingly sent data to the user's browser. But you'd get one of those "headers already sent" messages. Are you getting one of those? Note: People can append a PHPSESSID onto the end of your url to make you think a session is already started. Also when you destroy a session, the browser still likes to send the session ID to you. So checking for only the existance of PHPSESSID to see if the user has logged in or at least tried to is a bad idea. You should probably check for the existance of a variable inside that session. To see if they've attempted to login (even if you do that anyway, it's a good idea not to bother with something like if (!$PHPSESSID)). -- Plutarck Should be working on something... ...but forgot what it was. "Scott" <xjab@bellsouth.net> wrote in message news:B706080C.C64%xjab@bellsouth.net... > Does anyone know of a way to use sessions and still be able to redirect with > the header function. In the code below I test for the existence of the > PHPSESSID and send the user to register if it doesn't exist. If it does > exist I start a session to retrieve the session variables then call a class > that tests for other conditions. If the test is successful then I try to > redirect which fails because (as I understand it) session_start has already > sent headers. > I really don't like putting links on pages that say "Click here to continue" > so I'm looking for a solution. Any ideas? > > <? > if (!isset($PHPSESSID)){ > > header("Location:http://www.blah.com/register.php"); > } > session_start(); > if ($action == "update"){ > include("../includes/update_class.php"); > $my=new update_listing(); > header("Location:http://www.blah.com > mem_welcome.php?sid=$PHPSESSID"); > } > ?> > > JS Plauche > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#49619) next »