By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

help with session

P: n/a
Hello everyone,
I am having a brain fart or something, why won't this work?

test.php:

<?php
session_start();
$_SESSION['username']='test';
header("Location: test2.php");
exit();
?>

and test2.php:

<?php
session_start();
echo $_SESSION['username'];
?>

Any hint appreciated.

Jun 19 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
st***********@hotmail.com wrote:
Hello everyone,
I am having a brain fart or something, why won't this work?

test.php:

<?php
session_start();
$_SESSION['username']='test';
header("Location: test2.php");
exit();
?>

and test2.php:

<?php
session_start();
echo $_SESSION['username'];
?>

Any hint appreciated.


Hi,

Looks fine to me.
Is it possible PHP cannot use sessions at all because of a misconfigured
php.ini?
Can PHP read/write to the directory where sessions are stored (often /tmp)?
Did you accidentally set the session_safe_handler to user?

Regards,
Erwin Moller
Jun 19 '06 #2

P: n/a
st***********@hotmail.com wrote:
Hello everyone,
I am having a brain fart or something, why won't this work?

test.php:

<?php
session_start();
$_SESSION['username']='test'; session_write_close(); // <-- add this header("Location: test2.php");
exit();
?>

and test2.php:

<?php
session_start();
echo $_SESSION['username'];
?>

Any hint appreciated.


Jun 19 '06 #3

P: n/a
David Haynes wrote:
st***********@hotmail.com wrote:
Hello everyone,
I am having a brain fart or something, why won't this work?

test.php:

<?php
session_start();
$_SESSION['username']='test';

session_write_close(); // <-- add this


Hi David,

Why should that help?

according to php.net:

session_write_close

(PHP 4 >= 4.0.4, PHP 5)
session_write_close -- Write session data and end session
Description
void session_write_close ( void )

End the current session and store session data.

Session data is usually stored after your script terminated without the need
to call session_write_close(), but as session data is locked to prevent
concurrent writes only one script may operate on a session at any time.
When using framesets together with sessions you will experience the frames
loading one by one due to this locking. You can reduce the time needed to
load all the frames by ending the session as soon as all changes to session
variables are done.
I interpret that as a possibility to quickly close the session if you know
that you won't need it for the remainder of the script, so concurent
request on the same session are not put in line to wait for the first
script to finish.

But since the OP is terminating the script test.php by calling exit(); it
won't help. I think.
I am missing something about session_write_close() maybe?

Regards,
Erwin Moller

header("Location: test2.php");
exit();
?>

and test2.php:

<?php
session_start();
echo $_SESSION['username'];
?>

Any hint appreciated.


Jun 19 '06 #4

P: n/a
Erwin Moller wrote:
David Haynes wrote:
st***********@hotmail.com wrote:
Hello everyone,
I am having a brain fart or something, why won't this work?

test.php:

<?php
session_start();
$_SESSION['username']='test';

session_write_close(); // <-- add this


Hi David,

Why should that help?

according to php.net:
>
session_write_close

(PHP 4 >= 4.0.4, PHP 5)
session_write_close -- Write session data and end session
Description
void session_write_close ( void )

End the current session and store session data.

Session data is usually stored after your script terminated without the need
to call session_write_close(), but as session data is locked to prevent
concurrent writes only one script may operate on a session at any time.
When using framesets together with sessions you will experience the frames
loading one by one due to this locking. You can reduce the time needed to
load all the frames by ending the session as soon as all changes to session
variables are done.

I interpret that as a possibility to quickly close the session if you know
that you won't need it for the remainder of the script, so concurent
request on the same session are not put in line to wait for the first
script to finish.

But since the OP is terminating the script test.php by calling exit(); it
won't help. I think.
I am missing something about session_write_close() maybe?

Regards,
Erwin Moller


I'm a 'belt and suspenders' kind of guy when it comes to this. By using
session_write_close() I know that the data has been flushed into the
cookie prior to ending the program. It just removes one more variable in
the mix.

Having said that, the problem's symptoms could also be explained by
having a setup that is blocking cookies. The session_write_close() is
low hanging fruit so why not try it first?

-david-

Jun 19 '06 #5

P: n/a
David Haynes wrote:
Erwin Moller wrote:
David Haynes wrote:
st***********@hotmail.com wrote:
Hello everyone,
I am having a brain fart or something, why won't this work?

test.php:

<?php
session_start();
$_SESSION['username']='test';
session_write_close(); // <-- add this
Hi David,

Why should that help?

according to php.net:
>>
session_write_close

(PHP 4 >= 4.0.4, PHP 5)
session_write_close -- Write session data and end session
Description
void session_write_close ( void )

End the current session and store session data.

Session data is usually stored after your script terminated without the
need to call session_write_close(), but as session data is locked to
prevent concurrent writes only one script may operate on a session at any
time. When using framesets together with sessions you will experience the
frames loading one by one due to this locking. You can reduce the time
needed to load all the frames by ending the session as soon as all
changes to session variables are done.

I interpret that as a possibility to quickly close the session if you
know that you won't need it for the remainder of the script, so concurent
request on the same session are not put in line to wait for the first
script to finish.

But since the OP is terminating the script test.php by calling exit(); it
won't help. I think.
I am missing something about session_write_close() maybe?

Regards,
Erwin Moller


Hi David,
I'm a 'belt and suspenders' kind of guy when it comes to this. By using
session_write_close() I know that the data has been flushed into the
cookie prior to ending the program. It just removes one more variable in
the mix.

Having said that, the problem's symptoms could also be explained by
having a setup that is blocking cookies. The session_write_close() is
low hanging fruit so why not try it first?
Thanks for your reply.
I am also a belt and suspenders kind of programmer, but this functioncall
just doesn't make sense to me in this situation.

But as fas as I understand the matter: It will only write the sessiondata
away and close the session. But both actions will happen when the script
ends.

Of course, it is entirely possible I am missing something.

Anyway, let's wait what the OP has to say about it.

Regards,
Erwin Moller

-david-



Jun 19 '06 #6

P: n/a
ED

"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:44***********************@news.xs4all.nl...
David Haynes wrote:
Erwin Moller wrote:
David Haynes wrote:

st***********@hotmail.com wrote:
> Hello everyone,
> I am having a brain fart or something, why won't this work?
>
> test.php:
>
> <?php
> session_start();
> $_SESSION['username']='test';
session_write_close(); // <-- add this

Hi David,

Why should that help?

according to php.net:
>>>
session_write_close

(PHP 4 >= 4.0.4, PHP 5)
session_write_close -- Write session data and end session
Description
void session_write_close ( void )

End the current session and store session data.

Session data is usually stored after your script terminated without the
need to call session_write_close(), but as session data is locked to
prevent concurrent writes only one script may operate on a session at
any
time. When using framesets together with sessions you will experience
the
frames loading one by one due to this locking. You can reduce the time
needed to load all the frames by ending the session as soon as all
changes to session variables are done.

I interpret that as a possibility to quickly close the session if you
know that you won't need it for the remainder of the script, so
concurent
request on the same session are not put in line to wait for the first
script to finish.

But since the OP is terminating the script test.php by calling exit();
it
won't help. I think.
I am missing something about session_write_close() maybe?

Regards,
Erwin Moller


Hi David,
I'm a 'belt and suspenders' kind of guy when it comes to this. By using
session_write_close() I know that the data has been flushed into the
cookie prior to ending the program. It just removes one more variable in
the mix.

Having said that, the problem's symptoms could also be explained by
having a setup that is blocking cookies. The session_write_close() is
low hanging fruit so why not try it first?


Thanks for your reply.
I am also a belt and suspenders kind of programmer, but this functioncall
just doesn't make sense to me in this situation.

But as fas as I understand the matter: It will only write the sessiondata
away and close the session. But both actions will happen when the script
ends.

Of course, it is entirely possible I am missing something.

Anyway, let's wait what the OP has to say about it.

Regards,
Erwin Moller

-david-



hi guys,

Actually had the same issue as the op and using session_write_close did
indeed solve the problem.

The user contributed notes at:
http://uk.php.net/manual/en/function...rite-close.php
may shed some light here. Basically these suggest that you should call
session_write_close prior to the Location header to ensure that the session
file is correctly updated prior to the redirect (otherwise presumably, the
session file could still be locked/being written from the previous page).

cheers,
ED

Jun 19 '06 #7

P: n/a
Erwin Moller wrote:
<snip>
But since the OP is terminating the script test.php by calling exit(); it
won't help. I think.
I am missing something about session_write_close() maybe?

<snip>

session_write_close() will be implicitly called when you use
exit(); but as header redirection is above exit() and some time the
session values may not be written to the desk when that page redirect
is accessed.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 20 '06 #8

P: n/a
R. Rajesh Jeba Anbiah wrote:
Erwin Moller wrote:
<snip>
But since the OP is terminating the script test.php by calling exit(); it
won't help. I think.
I am missing something about session_write_close() maybe? <snip>

session_write_close() will be implicitly called when you use
exit(); but as header redirection is above exit() and some time the
session values may not be written to the desk when that page redirect
is accessed.


Hi Rajesh,

Aaah. Thanks, but I am still confused. :-/

Can i summarize it as follows?
When the exit() is reached, the session-writing and closing can be suspended
for some time (due to heavy load on the server eg.) and the next request
(originating from the header("Location: ...") is already trying to use that
same session.

But still I do not understand what can go wrong, since as I understand it
the second page is waiting for the first to finish before it can access the
session, so it hangs and waits.
So no problem there as far as I can see.

I would love to hear more opinions because I never use
session_write_close(), but never had any sessiontroubles either, not with
default sessionhandling (php/file) and custom (user/database).

So I would love to know if it is a serious improvement, or just a myth.

Can you/anybody shed some light on that?

TIA

Regards,
Erwin Moller

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/


Jun 20 '06 #9

P: n/a
st***********@hotmail.com wrote:
Hello everyone,
I am having a brain fart or something, why won't this work?

test.php:

<?php
session_start();
$_SESSION['username']='test';
header("Location: test2.php");
exit();
?>


Can you set a cookie in 302 response?

I'd look at the traffic (squid / ethereal / iehttpheaders / firefox
tamperData) and see. Or start the sessions before loaing test.php

C.
Jun 21 '06 #10

P: n/a
Erwin Moller wrote:
<snip>
I would love to hear more opinions because I never use
session_write_close(), but never had any sessiontroubles either, not with
default sessionhandling (php/file) and custom (user/database).

So I would love to know if it is a serious improvement, or just a myth.

<snip>

On session_start(), the page gets the session variables and on
exit() or implicit exit or session_write_close(), it writes back the
variables to session files--but there is a locking involved; unless any
of these are happened, any of the PHP files that tries to use the same
session/init the same session will get hanged. AFAIK, when using custom
session handler there is no locking involved.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 25 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.