473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

help with session

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
10 1398
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: james | last post by:
I am new to php and need some help getting the session variables into include files. (after-thought, Sorry for the drawn out post but I really, really need help....;) Here's what I'm doing.. ...
2
by: Alexandre MELARD | last post by:
Hi, My name is alexandre, I am 4th year student at the Napier university of edinburgh. I am finishing my year and do a presentation of my honours project next wednesday (the 5th of May). I am...
6
by: -D- | last post by:
I'm trying to accomplish the following. I'm trying to get the values for the table rows that are dynamically created to persist through a redirect. Referring URL:...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
5
by: Marc Bishop | last post by:
I'm trying to get this to work but i'm at a loss as to how. i've searched google without much help code Dim ArrCart As new ArrayList() ArrCart = CType(Session("sesCart"), ArrayList)...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
2
by: Orgil | last post by:
I'm using ASP 3.0 however there is ASP.NET, because I'm working an old site that is built in ASP 3.0. I hope you for getting any help for my problem from you. So, my site's sessions are empty...
3
by: jambonjamasb | last post by:
Hi I have two tables: email_tbl Data_table Data table is is used to create a Form Data_form
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.