Posted on 2005-05-03
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
    LVL 7

    Expert Comment

    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

    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

    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 ( 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.
    LVL 20

    Expert Comment

    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

    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

    PAQed with points refunded (500)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    mysql_connect PHP 7 13 403
    Webmin Bandwidth Monitoring not working 10 66
    Recompile PHP 5.5 with zip support for a newbe 8 94
    htaccess file 3 49
    As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
    In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now