1

I have a page that lazy loads photos via AJAX. Sometimes, as photos are loading, all session data is lost. It does not happen every time I load the page. I feel that I have ruled out session timeout/garbage collection as a cause. I also feel that I am returning the response from the AJAX requests correctly. The session ID is not changing, and the session cookie is not lost. All data saved to the session is simply gone. Does anyone have any ideas? I am using Laravel 4.1.28 and jQuery 1.8.0.

HTML snippet:

<tbody>
    <tr>
        <td class="photo-col" id="pidm-123456-photo-col"><img class="thumbnail" height="108" src="/shared/assets/img/idcardphotos/loading.gif" width="108"></td>
    </tr>
    <tr>
        <td class="photo-col" id="pidm-234567-photo-col"><img class="thumbnail" height="108" src="/shared/assets/img/idcardphotos/loading.gif" width="108"></td>
    </tr>

JavaScript:

$(document).ready(function() {
    loadPhotos();
});

function loadPhoto(pidm) {
    var img =
        $('<img class="thumbnail" height="108" width="108">')
            .attr('src', '/idcardphoto/' + pidm + '?thumbnail=true')
            .load(function() {
                $('#pidm-' + pidm + '-photo-col').html(img);
            });
}

function loadPhotos() {
    $('.photo-col').each(function() {
        loadPhoto($(this).attr('id').split('-')[1]);
    });
}

Route used by AJAX requests:

$this->app['router']->get('idcardphoto/{pidm}', ['as' => 'core::id_card_photo', function($pidm)
{
    $thumbnail = (bool) Input::get('thumbnail');

    $photo = '/path/to/idcardphotos/' . ($thumbnail ? 'thumbnails/' : '') . $pidm . '.jpg';

    if (file_exists($photo))
    {
        return Response::make(file_get_contents($photo))->header('Content-Type', 'image/jpg');
    }

    $photo = '/shared/assets/img/idcardphotos/notavailable.gif';

    return Response::make(file_get_contents($photo))->header('Content-Type', 'image/gif');
}])
    ->where('pidm', '[0-9]+');

Laravel session config:

return [
    'driver' => 'file',
    'lifetime' => 30,
    'expire_on_close' => true,
    'files' => storage_path().'/sessions',
    'connection' => null,
    'table' => 'sessions',
    'lottery' => array(2, 100),
    'cookie' => 'recreg_session',
    'path' => '/',
    'domain' => null,
    'secure' => false,
];

1 Answer 1

3

After further research, I now realize that this behavior is due to the fact that the Laravel file session driver does not perform any locking. I created a custom file session driver that does perform locking, and the problem went away. Here are a couple of links that helped me:

Sign up to request clarification or add additional context in comments.

1 Comment

Original link on Github is down. github.com/laravel/framework/issues/4576 is a similar topic. Changing to the cookie based session driver is a quick fix for this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.