Posted on 2005-05-03
Medium Priority
Last Modified: 2010-03-04
I have two scripts, first:

<?php session_start();

$_SESSION["user_id"] = $record["user_id"];


and second:

<?php session_start();

if (!isset($_SESSION["user_id"])) echo “error”;


the second script is called like a 30 seconds after the first from the same browser window, sometimes this "if" in second script returns true, how is it possible that session isnt set? what can cause it? only sometimes.
Question by:Buffon

Expert Comment

ID: 13919874
Try session_register on this variables.

use print_r($_SESSION); to debug. Check that the cookie is set with the session every visit.
LVL 20

Expert Comment

ID: 13920629
1.  you may have the inactivity threshold set extremely low... if that were the case then you might be loosing the session between the first and last access.  This would also explain why it is an intermittent behavior... since the way session cookies get destroyed is a random probability for every cookie beyond a certain age.

2.  if you're disabling cookies or something like that between moment a and moment b, that could cause the problem... or if you are working across multiple servers, then you might not be properly registering the code.  N.B. using the IP versus the DNS name count as different servers.
LVL 11

Author Comment

ID: 13928427
2 Promethyl

I read the manual for command session_register. The problem is that they do not recommend using $_SESSION (or $HTTP_SESSION_VARS) together with session_register(), so in order to test this suggestion I will need to alter numerous scripts. So, I’ll check this option only when I’ll get desperate of trying everything else.
However, in user comments there (http://php.net/session_register) I read that they had problems using session variables with the same name as $_GET variables (like $_SESSION[‘user_id’] with $_GET[‘user_id’]) and that’s my case too. So I’ve renamed all my session variables and I will wait to see if the error reappears.
As for array printing for debug purposes, I do have an error handler to print all environment variables (using get_defined_vars()), but printing unset variables provides not much use J



2 virmaior

If my inactivity threshold was set extremely low, I would get same error very frequently. But in other places in my project I can be inactive for much more time than it takes between the problematic scripts and the session stays intact. So, it is probably not the low inactivity threshold issue, since this would cancel session each time after timeout.
As for cookies disabling, I cannot be sure what other users did on their computers while surfing in my site, but I generated this error too and I do not mess with cookies disabling in my browser (deleting cookies in the middle of session does not kills session variables too). If by “working across multiple servers” you mean hosting site on several servers, then this is not my case. I have everything hosted on one server.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 20

Expert Comment

ID: 13928623
1.  deleting cookies in the middle of a session may in fact kill the session.  it won't remove the file from the server, but you won't have the unique id to get to it any more.

2.  the inactivity threshold doesn't work quite like that.  It's inactivity PLUS a random chance.  Meaning that it doesn't kill every session that has been inactive for the specified time.  Rather, every time a session is started, there is a random chance that it will remove every session that has been inactive for the minimum time.

specifically look at:

if you read this, it explains why it would be sporadic.
LVL 11

Author Comment

ID: 13937403
1. I tried several times to delete the cookies during session, but session variables remained intact.

2. I read about session garbage collector, variable 'maxlifetime' which specifies the number of seconds after which data will be seen as garbage and cleaned up is set to 1440 secs on my server, so it is not the source of the problem. But if it were low value - it could be the reason why I was losing session variables indeed. I will keep that feature in mind in the future.

Accepted Solution

modulo earned 0 total points
ID: 14198046
PAQed with points refunded (500)

Community Support Moderator

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question