473,761 Members | 7,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Locatio n: test2.php");
exit();
?>

and test2.php:

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

Any hint appreciated.

Jun 19 '06 #1
10 1428
st***********@h otmail.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("Locatio n: 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_ha ndler to user?

Regards,
Erwin Moller
Jun 19 '06 #2
st***********@h otmail.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_c lose(); // <-- add this header("Locatio n: test2.php");
exit();
?>

and test2.php:

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

Any hint appreciated.


Jun 19 '06 #3
David Haynes wrote:
st***********@h otmail.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_c lose(); // <-- add this


Hi David,

Why should that help?

according to php.net:

session_write_c lose

(PHP 4 >= 4.0.4, PHP 5)
session_write_c lose -- Write session data and end session
Description
void session_write_c lose ( 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_c lose(), 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_c lose() maybe?

Regards,
Erwin Moller

header("Locatio n: 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***********@h otmail.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_c lose(); // <-- add this


Hi David,

Why should that help?

according to php.net:
>
session_write_c lose

(PHP 4 >= 4.0.4, PHP 5)
session_write_c lose -- Write session data and end session
Description
void session_write_c lose ( 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_c lose(), 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_c lose() maybe?

Regards,
Erwin Moller


I'm a 'belt and suspenders' kind of guy when it comes to this. By using
session_write_c lose() 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_c lose() 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***********@h otmail.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_c lose(); // <-- add this
Hi David,

Why should that help?

according to php.net:
>>
session_write_c lose

(PHP 4 >= 4.0.4, PHP 5)
session_write_c lose -- Write session data and end session
Description
void session_write_c lose ( 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_c lose(), 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_c lose() maybe?

Regards,
Erwin Moller


Hi David,
I'm a 'belt and suspenders' kind of guy when it comes to this. By using
session_write_c lose() 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_c lose() 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************ *************** *************** @spamyourself.c om> wrote in
message news:44******** *************** @news.xs4all.nl ...
David Haynes wrote:
Erwin Moller wrote:
David Haynes wrote:

st***********@h otmail.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_c lose(); // <-- add this

Hi David,

Why should that help?

according to php.net:
>>>
session_write_c lose

(PHP 4 >= 4.0.4, PHP 5)
session_write_c lose -- Write session data and end session
Description
void session_write_c lose ( 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_c lose(), 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_c lose() maybe?

Regards,
Erwin Moller


Hi David,
I'm a 'belt and suspenders' kind of guy when it comes to this. By using
session_write_c lose() 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_c lose() 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_c lose 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_c lose 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_c lose() maybe?

<snip>

session_write_c lose() 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_c lose() maybe? <snip>

session_write_c lose() 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("Locatio n: ...") 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_c lose(), 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***********@h otmail.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("Locatio n: 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1429
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.. I have a php request_form that I use 2 different ways: by itself (url directly to the form) and as an include to be displayed in existing php pages. This form uses POST to another php_script that performs serverside validation and db write. If all...
2
1656
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 doing a project of data mining, and I run a mysql database to store my data. the database structure:
6
1585
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: http://www.dwayneepps.com/abr_site/fundingrequest.asp If you click on the "New Transaction" button it will generate a new row for the user to input information. I call a javascript function when the user clicks the "New Transaction" button that redirects to another page that...
28
3306
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 make-believe challenge in order to avoid confusing the issue further. Suppose I was hosting a dinner and I wanted to invite exactly 12 guests from my neighborhood. I'm really picky about that... I have 12 chairs besides my own, and I want them all...
8
5479
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
5
2059
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) ArrCart.add(sID & ",1") Session("sesCart") = ArrCart So what i want to do is maintain my Arraylist of items in a session.
11
26637
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 and PEAR is in c:\wamp\php\pear I modified php.ini in the c:\wamp\php directory to reflect the actual path, but even stopping and restarting my server shows the c: \php5\pear path. I can't change it no matter what I do I also tried the...
2
1747
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 before its timeout. I have searched and checked for long time around this problem, but I can't fix the problem yet. The session timeout is 20 minutes in IIS 5.1 by default. I didn't set the timeout as "session.timeout=10" in codes of VBScript
3
3012
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
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.