How do I keep SESSION variables from disappearing?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
repstein
Forum Newbie
Posts: 5
Joined: Thu Aug 26, 2010 3:53 pm

How do I keep SESSION variables from disappearing?

Post 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
User avatar
bradbury
Forum Commoner
Posts: 40
Joined: Wed Aug 25, 2010 11:21 am
Location: Eugene, OR

Re: How do I keep SESSION variables from disappearing?

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How do I keep SESSION variables from disappearing?

Post 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')
);
repstein
Forum Newbie
Posts: 5
Joined: Thu Aug 26, 2010 3:53 pm

Re: How do I keep SESSION variables from disappearing?

Post 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')
);
repstein
Forum Newbie
Posts: 5
Joined: Thu Aug 26, 2010 3:53 pm

Re: How do I keep SESSION variables from disappearing?

Post 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.
repstein
Forum Newbie
Posts: 5
Joined: Thu Aug 26, 2010 3:53 pm

Re: How do I keep SESSION variables from disappearing?

Post 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.
repstein
Forum Newbie
Posts: 5
Joined: Thu Aug 26, 2010 3:53 pm

Re: How do I keep SESSION variables from disappearing?

Post 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?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How do I keep SESSION variables from disappearing?

Post 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?
Post Reply