Page 1 of 1

session_start() not loading existing session

Posted: Fri Apr 04, 2008 1:47 pm
by pesky
On the login script all goes as planned. The Session cookie with session ID and other $_SESSION[] vars is created and can be found in the session.save_path dir

At the end of login.php call to isset($_SESSION['user']) returns true. THEN the login script exits with the header('Location: index.php') where index.php again calls session_start() but this call creates a new 'guest' session instead of loading the existing logged in 'user' session that was created via login script.

Apparently cookie is not being read by php - why? The browsers is set to accept all cookies. Have I left anything out?

I am working on localhost with Apache 2.2.0 on XP Pro. PHP version 5.1.2. MySQL 5.0.18 (apache2triad version 1.5.4)

Thanks for your time and help in advance.

Re: session_start() not loading existing session

Posted: Fri Apr 04, 2008 2:26 pm
by pesky
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I simplified my index.php to simply display the existing session's variables:

Code: Select all

 
<?php
session_start();
 
foreach($_SESSION as $key => $value ){
   echo "<P>$key => $value<P>\n"; 
}
 
echo 'user.user_id: '.$_SESSION['user']->user_id.'<br>';
echo 'user.user_login: '.$_SESSION['user']->user_login.'<br>';
echo 'user.user_email: '.$_SESSION['user']->user_email.'<br>';
echo 'user_zip_code: '.$_SESSION['user']->user_zip_code.'<br>';
?>
 
I have double checked - the logged in session's cookie does retrieve from db and contains these values *but* the index.php's session_start() call does not load the existing session instead creates a new one. Pls help!!

I've double checked all session vars in php.ini as correct and read all abt php sessions but still can't figure out this issue...

any ideas why this is happening?


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: session_start() not loading existing session

Posted: Fri Apr 04, 2008 3:31 pm
by pesky
The way I see it, it could be either:

a) Session Cookie is not in the place where php is expecting to read current session cookie from

My php.ini (apache on Windows XP pro) has the following value:
session.save_path="\tmp"

Apache installed in: C:\apache2triad folder

AND the session_id cookie with the correct session data is created in the following folder:
C:\apache2triad\tmp

I even changed value of session.save_path="C:\apache2triad\tmp"
yielding no change - same behavior!

On login script db verifies the user and on login script

Code: Select all

isset($_SESSION['user']) returns true
*but* after header('Location: index.php') on index.php

Code: Select all

isset($_SESSION['user']) returns false
and the existing session is not loaded...

b) maybe the browser is unable to read cookies *but*
I am using IE 7.0.5 with 'accept all cookies' option selected

Am I overlooking any options here? All ideas/recommendations/suggestions are welcome!

Re: session_start() not loading existing session

Posted: Fri Apr 04, 2008 4:20 pm
by Christopher
On redirects you may need to call session_write_close() before the redirect to make the values available, because both pages are within the same request. Also, are you saving an object in the session? "$_SESSION['user']->user_login"

Re: session_start() not loading existing session

Posted: Fri Apr 04, 2008 4:38 pm
by pesky
Thanks for your response.

I tried your suggestion and here's what I am seeing - now two session cookies are being created:

a) first cookie file is the one that my login script creates whose file name starts with 'abcde' followed by alphanumeric session id generated

this cookie file has all the session values that login script retrieves from the db and that I try to access on the index.php page

b) the other cookie file name starts with 'sess' followed by alphanumeric session id generated

this cookie file is empty

both cookie files have different session ids

what does this imply?

Re: session_start() not loading existing session

Posted: Fri Apr 04, 2008 5:05 pm
by Christopher
I don't know about the cookies, but does your second page load the session data now?

Re: session_start() not loading existing session

Posted: Fri Apr 04, 2008 6:01 pm
by pesky
no it does not.

i did figure out the source of two cookies however :)

So once user logs in, my login script calls the regenerate_session_id() and so the $_SESSION['user'] logged in values are saved in this newly generated cookie for the logged in user

what is happening is that when we go to the next page, php does not care about the regenerated session id's cookie - it still goes off of the original cookie to which my code has written nothing.

of course the simplest solution would be to not regenerate id and as a quick fix that's what I did

i believe a little background of my code is reqd to understand the situation: for every visitor to my site a 'guest' session is generated, once the visitor logs in then a new 'user' session is generated with different permissions and access to different pages of the site. Reason for doing this: my site has various levels of membership.

No the site is not functional yet :) most of it is still in my head and some of it under development

if anyone understands why php does not go off of the newly generated session id's cookie - i would greatly appreciate a deeper understanding of this issue!

Thank you very much for all your help and time. Appreciate it!

Re: session_start() not loading existing session

Posted: Fri Apr 04, 2008 6:54 pm
by Christopher
As I said, when you redirect both pages are within the same HTTP request -- so there are some things you need to be aware of. I would recommend regenerating on the second page in this case.