Reputation: 1036
Ahoy StackOverflow,
I have come across this issue in my project: in short, from what I've gathered, is that the PHP script called via AJAX is not properly registering SESSION variables that were set at the top of the index.php
page. At first, I assumed that it was due to session locking, so I went ahead and added session_write_close()
, however, that didn't fix the issue.
This issue only occurs about 25% percent of the time after a new user session begins (ie: when a user logs in).
I went ahead and deleted 90% of the code to get the bug down to its bare minimum coding necessary to reproduce.
Bad result from Firebug via ajax.php
Expected result from Firebug via ajax.php
Note: Both results show the return of index's print_r($_SESSION)
as Array ( [userid] => 3724 [trialstatus] => 1 [trialtcompletions] => 0 [userlevel] => 5 )
which lets me know the issue isn't with the session being set on the index page.
Does anyone know a fix (perhaps not even code-wise, maybe even a server setting) that will properly allow the script called via AJAX to access the Session variable correctly?
Testing Scenario for Reproduction
index.php
<?php
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['userid']))
{
$_SESSION['userid'] = 3724; //$login['AccountID'];
}
$_SESSION['trialstatus'] = "12";
$_SESSION['trialtcompletions'] = "12";
$_SESSION['userlevel'] = "12";
session_write_close();
print_r($_SESSION);
?>
<!DOCTYPE html><html><head><script src="./js/jquery.min.js"></script>
<script>
function loadStage(step,input,callback){
$.ajax({
type: "POST",
url: "./ajax.php",
data: { step: step, input: input },
dataType: "JSON",
timeout: 5000,
success: function(data){
if(data !== false){
callback(data);
}
}
});
}
$(document).ready(function(){
startLoadingSequence();
});
function startLoadingSequence(skipped){
loadStage(1,skipped,function(data){});
}
</script>
</head></html>
ajax.php
<?php
if (session_status() === PHP_SESSION_NONE) session_start();
print_r($_SESSION);
if (!isset($_SESSION['userid']))
{
die(json_encode(array(
"error",
"You must be logged in to view report data."
)));
}
?>
Per request:
Read comments for extra information
Upvotes: 23
Views: 1557
Reputation: 31654
If you're running a load balancer then you have to make sure your servers are hitting a common point for data. By default PHP stores sessions in the local file system. That becomes a problem if your load balancer sends you from server A to server B, where that file doesn't exist. You could set up a network share and make sure all web servers use that share. So you could create an NFS share and then add session_save_path or set it within php.ini
session_save_path('/your/nfs/share/here');
Another option is to write your own session handler that puts sessions into your database. You could then use something like memcached to store the sessions in a way that you won't hammer your DB every time you read your session data.
Upvotes: 8
Reputation: 593
The screenshot I have attached is same for almost all 25-30 runs I have run. [1]: https://i.sstatic.net/MgLpS.jpg Check your session data size maybe it's exceeding the cache size. or some other session data is being stored exhausting your cache memory. Increase your cache size maybe it will help for your scenario.
Upvotes: 1
Reputation: 519
Two things can cause this issue.
Upvotes: 11