October CMS session deletes data after all PHP code is executed

71 Views Asked by At

I am having a problem where when i try to load data directly into the Session when the page loads, the data disappears on the next AJAX request. This led me to think that the session regenerates a token after the PHP code is executed on the onStart() function on the page.

This is my code on the onStart() function on the page:

function onStart() {
    Session::regenerate();
    
    // More code
}

My code to store the data:

function onStoreData() {
    $data = Input::all();
    
    $sessionData = [
        'value1' => $data['value1'],
        'value2' => $data['value2'],
        'value3' => $data['value3'],
        'value4' => $data['value4'],
    ];
    
    Session::put('sessionData', $sessionData);
    
    if(Session::get('sessionData') === $sessionData) {
        return ['success' => true];
    } else {
        return ['success' => false];
    }
}

My code to retrieve the data:

function onGetSessionData() {
    $sessionData = Session::get('sessionData');
    
    return $sessionData;
}

AJAX request to store the data:

function storeData() {
    let canContinue = true;
    
    let value1Value = value1.value,
        value2Value = value2.value,
        value3Value = value3.value,
        value4Value = value4.value;
        
    $.request('onStoreData', {
        data: {
            'value1': value1Value,
            'value2': value2Value,
            'value3': value3Value,
            'value4': value4Value
        },
        async: false,
        success: function(data) {
            canContinue = data['success'];
        },
        error: function() {
            canContinue = false;
        }
    });
    
    return canContinue;
}

AJAX request to retrieve the data:

function afterStoredData() {
    let canContinue = true;
    
    let sessionData = null;
    
    $.request('onGetSessionData', {
        async: false,
        success: function(data) {
            sessionData = data;
        }
    });
    
    // Some more code
    
    return canContinue;
}

I am calling these JavaScript functions with the following code:

$(document).ready(function() {
    // Some code
    
    if(isFromOtherPage == '') {
        firstInit();
    } else {
        secondInit();
        
        // setTimeout to try and solve the problem but no success
        setTimeout(() => {
            if(typeof storeData === 'function') {
                storeData();
            }
            if(typeof afterStoredData === 'function') {
                afterStoredData();
            }
        }, 500);
    }

    // Some more code
});

sessionData will always be null for some reason.

afterStoredData() will always be called only and only after storeData().

What I noticed when I dump the Session data is that the token changes. This works on my local machine but when running on the server it does not work.

I am using October CMS v1.x.

Would appreciate any help.

0

There are 0 best solutions below