Problem with sessions and redirecting

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
doorman3
Forum Newbie
Posts: 2
Joined: Sat Oct 20, 2007 6:34 pm

Problem with sessions and redirecting

Post 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]
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Problem with sessions and redirecting

Post 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... :)
doorman3
Forum Newbie
Posts: 2
Joined: Sat Oct 20, 2007 6:34 pm

Post 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 ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply