0

Please help this issue has been driving me crazy and I feel like I've researched everywhere and come up with no answers.

Description of the Problem
I am creating an intranet web application for my company using PHP, MySQL, Apache from WAMP.
I am having an issue where my $_SESSION data on the server is randomly getting deleted with no apparent reason (I'm sure there's a reason, I just can't catch it!!!).
What I have is an index.js file included in every HTML page displayed. This JS file has the following code:

var refreshTime = 60000;    // 1 minute = 1000ms/1s * 60s/min = 60,000
//  Function called every refreshTime interval
window.setInterval( function() {    
    //  AJAX call to refreshSession.php simply calls session_start() in PHP
    $.ajax({
        cache: false,
        type: "GET",
        url: rootWebHostURL + "php/refreshSession.php",
        success: function(data) {
            console.log("Session refreshed via AJAX call in index.js");
            console.log("Dumping RAW JSON DATA");
            console.log(data);

            testobj = jQuery.parseJSON(data);
            console.log("Dumping JSON PARSED DATA"); 
            console.log(testobj);

            if ( testobj.sessionIsEmpty )
            {
                alert('SESSION DATA IS EMPTY');
            }
        }
    });
}, refreshTime );

This portion makes an AJAX call to the server every 1 minute. The PHP code called, calls session_start() to refresh the session and keep it's last access time within the limits of gc_maxlifetime (which I have set to 3600s or 1 hour) in the php.ini file. I recently modified the code to make a browser alert whenever the session data is dropped.

The refreshSession.php code is shown below:

session_start();
$arrayForAJAX = session_get_cookie_params();
$arrayForAJAX['id'] = session_id();

$sessionLog = fopen('C:\\...\\Desktop\\AAPEC Project\\Log\\session_log_00.txt', 'a+');

$dateNow = date('Y-m-d H:i:s');

$textToWrite = $dateNow . PHP_EOL . '===================' . PHP_EOL;

$sessionDataString = var_export( $_SESSION, true );

$textToWrite .= $sessionDataString . PHP_EOL . '==========================================' . PHP_EOL . PHP_EOL;

fwrite($sessionLog, $textToWrite);

$arrayForAJAX['sessionIsEmpty'] = false;
if ( empty($_SESSION) )
{
    $arrayForAJAX['sessionIsEmpty'] = true;
}

echo json_encode( $arrayForAJAX, JSON_FORCE_OBJECT);

What I've recently done in the refreshSession.php is write the contents of $_SESSION variable to a file, with a timestamp, every time refreshSession.php is called from AJAX. See the contents of this 'log' file below.

2016-06-16 09:24:15
=================== array (   'workingStatus' => 'in_process',   'currentpage' => 'datacapture.tpl',   'visitedpages' =>    array (
    0 => 'stocknumber.tpl',
    1 => 'preflight.tpl',
    2 => 'general.tpl',
    3 => 'pxeactions.tpl',
    4 => 'timezone.tpl',
    5 => 'options.tpl',
    6 => 'datacapture.tpl',   ),   'stocknumber.tpl' =>    array (
    'stocknum' => '12345678',
    'tenDigitStockNumber' => '0012345678',   ),   'operationType' => 'new',   'preflight.tpl' =>    array (
    'hardware_class' => '0',
    'tot_sys_memory' => '1',
    'installed_os' => '0',
    'pxe_boot_method' => '0',   ),   'general.tpl' =>    array (
    'engineer' => 'Bill Hodges',
    'company_name' => 'af',
    'parent_pn' => 'afds',
    'network_location' => 'Images',
    'support_file_directory' => 'Images\\12345678',   ),   'pxeactions.tpl' =>    array (
    'pxe_boot_action' => '0',
    'pxe_boot_mode' => '0',   ),   'timezone.tpl' =>    array (
    'timezone' => '-7',   ),   'options.tpl' =>    array (
    'datacapture' => 'datacapture.tpl',
    'virusscan' => 'virusscan.tpl',   ),   'optionpages' =>    array (
    0 => 'datacapture.tpl',
    1 => 'virusscan.tpl',   ), )
==========================================

2016-06-16 09:25:15
=================== array (   'workingStatus' => 'in_process',   'currentpage' => 'datacapture.tpl',   'visitedpages' =>    array (
    0 => 'stocknumber.tpl',
    1 => 'preflight.tpl',
    2 => 'general.tpl',
    3 => 'pxeactions.tpl',
    4 => 'timezone.tpl',
    5 => 'options.tpl',
    6 => 'datacapture.tpl',   ),   'stocknumber.tpl' =>    array (
    'stocknum' => '12345678',
    'tenDigitStockNumber' => '0012345678',   ),   'operationType' => 'new',   'preflight.tpl' =>    array (
    'hardware_class' => '0',
    'tot_sys_memory' => '1',
    'installed_os' => '0',
    'pxe_boot_method' => '0',   ),   'general.tpl' =>    array (
    'engineer' => 'Bill Hodges',
    'company_name' => 'af',
    'parent_pn' => 'afds',
    'network_location' => 'Images',
    'support_file_directory' => 'Images\\12345678',   ),   'pxeactions.tpl' =>    array (
    'pxe_boot_action' => '0',
    'pxe_boot_mode' => '0',   ),   'timezone.tpl' =>    array (
    'timezone' => '-7',   ),   'options.tpl' =>    array (
    'datacapture' => 'datacapture.tpl',
    'virusscan' => 'virusscan.tpl',   ),   'optionpages' =>    array (
    0 => 'datacapture.tpl',
    1 => 'virusscan.tpl',   ), )
==========================================

2016-06-16 09:26:15
=================== array ( )
==========================================

I cut the contents of the file down here to save space. There were about 10+ successfuly $_SESSION data writes before the first line, but as you can see the last write was an empty array. Indicating that there is no $_SESSION data. I have attempted to use debug_backtrace() here to get information about where PHP was in the program and determine what scripts/functions were called that could be causing this problem. debug_backtrace(), however, returns an empty array when I tried using it in my refreshSession.php script. From my research, it indicated that this is because refreshSession.php is the first and only script called.

WHY?!?!? Why would this code make 10+ or more successful session_start() calls and save $_SESSION data and then randomly drop data on one of the calls?

I have the following settings in php.ini file that pertain to sessions/cookies:

session.gc_probability = 1
session.gc_divisor = 1
session.gc_maxlifetime = 3600
session.cookie_lifetime = 0


which I realize, with the way I am doing things, will effectively keep this session alive forever since I'm refreshing the session every 1 min. Which is fine for now, I just don't want data to drop out of nowhere in the middle of the app.

Side Notes:
I realize that cookie_lifetime = 0 will keep the cookie 'alive' on the browser until the browser is closed. I realize the combination of session.gc_xxx variables will produce a probability of 1 for session garbage collection, which if I understand correctly will clear out old sessions that have not been 'modified' in the past 1 hour (since session.gc_lifetime = 3600s). I have considered and even began writing code for user managed sessions using a DB to give me more control, but after more research, I do not think it is necessary for the simple task that I am trying to do here.

I am a complete novice at this stuff (recently graduated with a BSEE w/ Computer Information Systems minor, most of my knowledge about all this is from Google and StackOverflow) and have managed to create this web application, however I am by no means a web/php developer. I probably have tons of errors in what/how I'm doing things in my code above. Any constructive criticisms and links to improvement are appreciated as well.

stEEvie
  • 1
  • 4
  • http://stackoverflow.com/questions/1236374/session-timeouts-in-php-best-practices Be_Open's answer might help you out. I think you might have to change your settings in you php.ini for your garbage collector. – Sari Rahal Jun 16 '16 at 17:29
  • @SariRahal thanks for your response. I don't believe that Be_Open's answer is any help here. I think I have a solid understanding of the PHP garbage collection process. This is a low volume server probably a max of 10 users on at any one time. With my current settings on my local dev PC, the probability of garbage collection is 1. So every time I call session_start() which is every 1 minute, garbage collection runs and should delete session files that haven't been active in the past hour. However, since I refresh my session every 1 minute, the session data should never be deleted – stEEvie Jun 16 '16 at 17:41
  • @SariRahal and in fact, it never does get deleted. That session file always exists in my /tmp folder, however the data within it is what is getting deleted. Not sure why though!!!! – stEEvie Jun 16 '16 at 17:42
  • Have you tried using a named session in order to verify you don't have something like unset session being called from a different app on the same server? – devlin carnate Jun 16 '16 at 17:44
  • @devlincarnate No I have not. Do you have a link to start me in the right direction? – stEEvie Jun 16 '16 at 20:06
  • You just set the name, `session_name("foobar");` , before calling `session_start();` . The description on the [man page](http://php.net/manual/en/function.session-start.php) states this. If this fixes your problem, please let me know and I'll post it as an answer. I'm just stabbing in the dark at one possibility so I don't know if it's actually an answer for your problem. – devlin carnate Jun 16 '16 at 22:34

0 Answers0