Page 1 of 1
How do I keep SESSION variables from disappearing?
Posted: Thu Aug 26, 2010 3:55 pm
by repstein
For a new online test I developed (AreWeGoodTogether dt com), occasionally SESSION variables entered on one page disappear on a later page (14 scripts later), which sends people all the way back to beginning - not good. It happens, say, when someone is using Chrome on a Mac - but also sometimes when someone is using Firefox on a PC - to maybe 1 in 100 people.
To deal with users who have their cookies turned off, I added:
ini_set('session.use_trans_sid', true);
before
session_start();
in every program, but that hasn't completely solved the problem. Any suggestions about how I can keep SESSION variables from disappearing? Thanks, Robert
Re: How do I keep SESSION variables from disappearing?
Posted: Thu Aug 26, 2010 4:49 pm
by bradbury
Hmm that sounds a little bit funny since a session doesnt end until the call is made for the session to be stopped. Check your code again and make sure that the session_start() really is at the beginning of EVERY single file that is using any of the session variables. If they are all there like you have said it may just be a browser error with the registration of SESSIONS.
Re: How do I keep SESSION variables from disappearing?
Posted: Thu Aug 26, 2010 4:57 pm
by McInfo
This is just a guess. Check these settings:
Code: Select all
<?php
header('Content-Type: text/plain');
var_dump(
ini_get('session.cookie_lifetime'),
ini_get('session.gc_maxlifetime'),
ini_get('session.gc_probability'),
ini_get('session.gc_divisor')
);
Re: How do I keep SESSION variables from disappearing?
Posted: Sun Aug 29, 2010 2:37 pm
by repstein
Thank, McInfo - That gives:
string(1) "0"
string(6) "200000"
string(1) "1"
string(9) "100000000"
Are you suggesting that I temporarily increase cookie lifetime in php.ini using "session_set_cookie_params"?
Meanwhile, I've started using a brute-force approach to the problem - forcing the session ID to move from script to script using the POST method:
I'm using:
input type='hidden' name='PHPSESSID' value='$PHPSESSID'
in one script, then using:
$PHPSESSID = $_POST['PHPSESSID'];
in the next script. It seems silly, but it also seems to be working.
----------------------------
Re: How do I keep SESSION variables from disappearing?
Post by McInfo ยป Thu Aug 26, 2010 2:57 pm
This is just a guess. Check these settings:
<?php
header('Content-Type: text/plain');
var_dump(
ini_get('session.cookie_lifetime'),
ini_get('session.gc_maxlifetime'),
ini_get('session.gc_probability'),
ini_get('session.gc_divisor')
);
Re: How do I keep SESSION variables from disappearing?
Posted: Sun Aug 29, 2010 2:41 pm
by repstein
bradbury wrote:Hmm that sounds a little bit funny since a session doesnt end until the call is made for the session to be stopped. Check your code again and make sure that the session_start() really is at the beginning of EVERY single file that is using any of the session variables. If they are all there like you have said it may just be a browser error with the registration of SESSIONS.
Thanks. Every program that uses the SESSION vars starts with:
<?php
ini_set('session.use_trans_sid', true);
session_start();
I'm using "ini_set('session.use_trans_sid', true);" to deal with people who have their cookies turned off.
Re: How do I keep SESSION variables from disappearing?
Posted: Sun Aug 29, 2010 2:44 pm
by repstein
McInfo wrote:This is just a guess. Check these settings:
Code: Select all
<?php
header('Content-Type: text/plain');
var_dump(
ini_get('session.cookie_lifetime'),
ini_get('session.gc_maxlifetime'),
ini_get('session.gc_probability'),
ini_get('session.gc_divisor')
);
Thank, McInfo - That gives:
string(1) "0"
string(6) "200000"
string(1) "1"
string(9) "100000000"
Are you suggesting that I temporarily increase cookie lifetime in php.ini using "session_set_cookie_params"?
Meanwhile, I've started using a brute-force approach to the problem - forcing the session ID to move from script to script using the POST method:
I'm using:
input type='hidden' name='PHPSESSID' value='$PHPSESSID'
in one script, then using:
$PHPSESSID = $_POST['PHPSESSID'];
in the next script. It seems silly, but it also seems to be working.
Re: How do I keep SESSION variables from disappearing?
Posted: Sun Aug 29, 2010 3:04 pm
by repstein
McInfo wrote:This is just a guess. Check these settings:
Code: Select all
<?php
header('Content-Type: text/plain');
var_dump(
ini_get('session.cookie_lifetime'),
ini_get('session.gc_maxlifetime'),
ini_get('session.gc_probability'),
ini_get('session.gc_divisor')
);
Okay, I've tried adding:
ini_set('session.cookie_lifetime', 20000);
to every program that uses SESSION vars.
I'll see if that helps. For redundancy, I could also add that instruction to php.ini. What do you think?
Re: How do I keep SESSION variables from disappearing?
Posted: Sun Aug 29, 2010 4:54 pm
by McInfo
I think you should leave session.cookie_lifetime at 0 so (in theory) the cookie will remain until the browser is closed.
If practical, it is preferable to make changes directly to php.ini rather than with ini_set() because some settings cannot be changed at runtime.
Problems that occur 1 in 100 times must be solved theoretically rather than with trial and error. Unfortunately, that makes them more difficult to solve than problems that recur more frequently.
How do you know this is happening? Do users report it? Have you experienced it yourself? Does it always happen on the same page? Does it always happen after the same period of time?