dsadsadsa1231 2016-06-16 17:21
浏览 46

PHP会话数据随机获取删除

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 目详情-五一模拟赛详情页
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line