Page 1 of 1

Problem with sessions and redirecting

Posted: Sat Oct 20, 2007 6:57 pm
by doorman3
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi!!

I am making a very simple login using sessions. 
The login file is called login.php. Here is the code :

Code: Select all

<?
       session_start();
       $_SESSION["logged_user"]='john';
       $_SESSION["logged_pass"]='password';
       $ID = session_id();
       header("location:index.php?PHPSESSID=$ID");		
?>
Next I want to print the session variables but now from the index.php file. Here is the code :

Code: Select all

<?
        session_start();
        $PHPSESSID = $_GET["PHPSESSID"];
        session_id($PHPSESSID);		       		
        echo $_SESSION["logged_user"];
?>
However it seems the session is lost and nothing is printed.
I spent the whole day trying to figure this out but without any luck.
Any idea what might be wrong ?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: Problem with sessions and redirecting

Posted: Sat Oct 20, 2007 7:08 pm
by alex.barylski

Code: Select all

<?
       session_start();
       $_SESSION["logged_user"]='john';
       $_SESSION["logged_pass"]='password';
       header("location: index.php");		
?>

<?
        session_start();
        echo $_SESSION["logged_user"];
?>

SESSION library will automatically handle propagation of the ID via URL if nessecary, otherwise it stores the PHPSESSID in cookies where it's best kept.

Try the above code... :)

Posted: Sun Oct 21, 2007 4:55 am
by doorman3
Hi Hockey, thanks for your reply.

I tried the above code but still no luck. It looks like the sessions are not working on my computer, any suggestions ??

Posted: Mon Oct 22, 2007 4:56 pm
by feyd
Header based redirection can and does short-circuit other headers (such as those which set cookies) from happening. session_write_close() may be of interest. session_name() should also be of interest.