Welcome! Log In Create A New Profile

Advanced

[PHP] session lost problem

Posted by bug zhu 
bug zhu
[PHP] session lost problem
April 24, 2012 07:00AM
hi all:

there are tow php files a.php and b.php,

content of a.php as follows:
<?php
session_start();
if (!isset($_GET['flag']))
{
header('Location: b.php');
}
else
{
var_dump($_SESSION);
}

content of b.php as follows:
<?php
session_start();
session_commit();
$_SESSION['test'] = 'test';
session_commit();
header('Location: a.php?flag=1');

when i visit a.php, the dumped $_SESSION array is empty
but if i commented the first session_commit() in b.php and then visit
a.php, i cound see the $_SESSION array,which is not empty
i wish i have descibed clear about my problem and someone could give me a
feedback~

--

thanks,
bugzhu
Stuart Dallas
Re: [PHP] session lost problem
April 24, 2012 07:30AM
On 24 Apr 2012, at 05:58, bug zhu wrote:

> there are tow php files a.php and b.php,
>
> content of a.php as follows:
> <?php
> session_start();
> if (!isset($_GET['flag']))
> {
> header('Location: b.php');
> }
> else
> {
> var_dump($_SESSION);
> }
>
> content of b.php as follows:
> <?php
> session_start();
> session_commit();
> $_SESSION['test'] = 'test';
> session_commit();
> header('Location: a.php?flag=1');
>
> when i visit a.php, the dumped $_SESSION array is empty
> but if i commented the first session_commit() in b.php and then visit
> a.php, i cound see the $_SESSION array,which is not empty
> i wish i have descibed clear about my problem and someone could give me a
> feedback~


I'm really not clear on what you're trying to do here, but the behaviour you're describing is as designed. When you call session_commit() you are saving and closing the session, so nothing done to $_SESSION after that point will be saved, even if you call session_commit() again.

-Stuart

--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stuart Dallas
Re: [PHP] session lost problem
April 24, 2012 07:50AM
Please don't top-post, and please include the list when replying.

On 24 Apr 2012, at 06:35, bug zhu wrote:
> 2012/4/24 Stuart Dallas <[email protected]>
>> On 24 Apr 2012, at 05:58, bug zhu wrote:
>>
>> > there are tow php files a.php and b.php,
>> >
>> > content of a.php as follows:
>> > <?php
>> > session_start();
>> > if (!isset($_GET['flag']))
>> > {
>> > header('Location: b.php');
>> > }
>> > else
>> > {
>> > var_dump($_SESSION);
>> > }
>> >
>> > content of b.php as follows:
>> > <?php
>> > session_start();
>> > session_commit();
>> > $_SESSION['test'] = 'test';
>> > session_commit();
>> > header('Location: a.php?flag=1');
>> >
>> > when i visit a.php, the dumped $_SESSION array is empty
>> > but if i commented the first session_commit() in b.php and then visit
>> > a.php, i cound see the $_SESSION array,which is not empty
>> > i wish i have descibed clear about my problem and someone could give me a
>> > feedback~
>>
>>
>> I'm really not clear on what you're trying to do here, but the behaviour you're describing is as designed. When you call session_commit() you are saving and closing the session, so nothing done to $_SESSION after that point will be saved, even if you call session_commit() again.
>
> but in a single file without redirect, code as follows
> <?php
> session_start();
> session_commit();
> $_SESSION['test'] = 'test';
> session_commit();
> var_dump($_SESSION);
>
> could dump the $_SESSION array.

Yes, because $_SESSION is not special in any way other than that it's used by the session system; it's no more than a superglobal array. So within one request that array contains whatever you put into it, but that doesn't mean it is stored in whatever session storage mechanism you're using (files by default). When you redirect to another URL that's a whole new request so the contents of $_SESSION have to be loaded from the session storage.

-Stuart

--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Adam Richardson
Re: [PHP] session lost problem
April 24, 2012 08:10AM
On Tue, Apr 24, 2012 at 12:58 AM, bug zhu <[email protected]> wrote:
> there are tow php files a.php and b.php,
>
> content of a.php as follows:
> <?php
> session_start();
> if (!isset($_GET['flag']))
> {
> header('Location: b.php');
> }
> else
> {
> var_dump($_SESSION);
> }
>
> content of  b.php as follows:
> <?php
> session_start();
> session_commit();
> $_SESSION['test'] = 'test';
> session_commit();
> header('Location: a.php?flag=1');
>
> when i visit a.php, the dumped $_SESSION array is empty
> but if i commented the first session_commit() in b.php and then visit
> a.php, i cound see the $_SESSION array,which is not empty
> i wish i have descibed  clear about my problem and someone could give me a
> feedback~

Hi,

So, you:
1) Visit page a.php (I'm assuming without the flag)
2) Are forwarded to page b.php, which you're expecting to store a
session variable 'test'.
3) Then forwarded back to page a.php.

You're likely expecting that you're return visit to page a.php should
reveal the 'test' variable.

The issue is that you're calling session_commit(), which is actually
an alias for session_write_close(). This function actually stops the
current session. So, when you hit the line $_SESSION['test'] = 'test',
your session has already terminated.

Try removing the session_commit() calls (or at least permanently
remove the first call.) You only want to call session_commit() when
you're done accessing/updating $_SESSION variables.

Adam

--
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
bug zhu
Re: [PHP] session lost problem
April 24, 2012 08:30AM
thank you for your explanation,
when i write to $_SESSION after session_commit(),$_SESSION is just a
regular array

2012/4/24 Stuart Dallas <[email protected]>

> Please don't top-post, and please include the list when replying.
>
> On 24 Apr 2012, at 06:35, bug zhu wrote:
> > 2012/4/24 Stuart Dallas <[email protected]>
> >> On 24 Apr 2012, at 05:58, bug zhu wrote:
> >>
> >> > there are tow php files a.php and b.php,
> >> >
> >> > content of a.php as follows:
> >> > <?php
> >> > session_start();
> >> > if (!isset($_GET['flag']))
> >> > {
> >> > header('Location: b.php');
> >> > }
> >> > else
> >> > {
> >> > var_dump($_SESSION);
> >> > }
> >> >
> >> > content of b.php as follows:
> >> > <?php
> >> > session_start();
> >> > session_commit();
> >> > $_SESSION['test'] = 'test';
> >> > session_commit();
> >> > header('Location: a.php?flag=1');
> >> >
> >> > when i visit a.php, the dumped $_SESSION array is empty
> >> > but if i commented the first session_commit() in b.php and then visit
> >> > a.php, i cound see the $_SESSION array,which is not empty
> >> > i wish i have descibed clear about my problem and someone could give
> me a
> >> > feedback~
> >>
> >>
> >> I'm really not clear on what you're trying to do here, but the
> behaviour you're describing is as designed. When you call session_commit()
> you are saving and closing the session, so nothing done to $_SESSION after
> that point will be saved, even if you call session_commit() again.
> >
> > but in a single file without redirect, code as follows
> > <?php
> > session_start();
> > session_commit();
> > $_SESSION['test'] = 'test';
> > session_commit();
> > var_dump($_SESSION);
> >
> > could dump the $_SESSION array.
>
> Yes, because $_SESSION is not special in any way other than that it's used
> by the session system; it's no more than a superglobal array. So within one
> request that array contains whatever you put into it, but that doesn't mean
> it is stored in whatever session storage mechanism you're using (files by
> default). When you redirect to another URL that's a whole new request so
> the contents of $_SESSION have to be loaded from the session storage.
>
> -Stuart
>
> --
> Stuart Dallas
> 3ft9 Ltd
> http://3ft9.com/
>



--

thanks,
bugzhu
[email protected]
Re: [PHP] session lost problem
April 24, 2012 09:30AM
bug zhu <[email protected]> hat am 24. April 2012 um 08:28 geschrieben:

> thank you for your explanation,
> when i write to $_SESSION after session_commit(),$_SESSION is just a
> regular array

Yes. Actually session_commit does not "terminate" the session as mentioned
earlier but is closes it for writing. You cann still read session values.

The benefit of using session_commit is that the server saved associated
session file is no longer locked, so that parallel requests can both access
the values.

The approach ist as follows:
Call session_commit() as early in you code (after session_open) as possible
to avoid locking. So first do all the writing to the $_SESSION array, then
do write close (or commit). After that you can still read all session
relevant information.

If you want to write afterwards to your $_SESSIOn array you simply have to
call session_start to re-open the write context. Afterwards you can commit
it again to remove the lock.

But be careful! session_start and session_commit perform write operations
on your harddisk or whatever storage you use. Many calls to start and
commit will result in losing performance.

Regards,
Marco

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
bug zhu
Re: [PHP] session lost problem
April 24, 2012 10:10AM
2012/4/24 marco@behnke.biz <[email protected]>

>
>
> bug zhu <[email protected]> hat am 24. April 2012 um 08:28 geschrieben:
>
> > thank you for your explanation,
> > when i write to $_SESSION after session_commit(),$_SESSION is just a
> > regular array
>
> Yes. Actually session_commit does not "terminate" the session as mentioned
> earlier but is closes it for writing. You cann still read session values.
>
> The benefit of using session_commit is that the server saved associated
> session file is no longer locked, so that parallel requests can both access
> the values.
>
> The approach ist as follows:
> Call session_commit() as early in you code (after session_open) as possible
> to avoid locking. So first do all the writing to the $_SESSION array, then
> do write close (or commit). After that you can still read all session
> relevant information.
>
> If you want to write afterwards to your $_SESSIOn array you simply have to
> call session_start to re-open the write context. Afterwards you can commit
> it again to remove the lock.
>
> But be careful! session_start and session_commit perform write operations
> on your harddisk or whatever storage you use. Many calls to start and
> commit will result in losing performance.
>
> Regards,
> Marco
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
got it,
very appreciate you explanation:-)

--

thanks,
bugzhu
Sorry, only registered users may post in this forum.

Click here to login