Search code examples
phpjqueryjsonajaxcloud-hosting

Cloud hosting causing issues with JSON retrieval?


I'm hosted with Smarthosting, they used cloud based hosting which delivers faster loading times - great!

But I have a snag.

I'm setting some sessions via PHP in a seperate file...

<?php
session_start();

if(filter_var($_POST['question_1'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_1'] = addslashes($_POST['question_1']);
}

if(filter_var($_POST['question_2a'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_2a'] = addslashes($_POST['question_2a']);
}
if(filter_var($_POST['question_2b'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_2b'] = addslashes($_POST['question_2b']);
}
if(filter_var($_POST['question_2c'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_2c'] = addslashes($_POST['question_2c']);
}
if(filter_var($_POST['question_2d'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_2d'] = addslashes($_POST['question_2d']);
}
if(filter_var($_POST['question_2e'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_2e'] = addslashes($_POST['question_2e']);
}
if(filter_var($_POST['question_2f'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_2f'] = addslashes($_POST['question_2f']);
}
if(filter_var($_POST['question_2g'], FILTER_VALIDATE_INT)) { 
    $_SESSION['question_2g'] = addslashes($_POST['question_2g']);
}
?>

Then later on I access another PHP file which puts these into a JSON string...

<?php
session_start();
echo json_encode($_SESSION);
?>

This works fine, however, until I call the JSON via Ajax...

$.getJSON( "retrieve-variables.php", function( data ) {
    var items = [];
    ...etc....

});

It's not pulling the most recent session data, it seems to pull back the session data from previous attempts. Is this to do with the cloud hosting? Or some other issue? Is there a way I can disable caching for this particular file and/or entire directory?

Thanks for listening.

EDIT: If I access the PHP retrieval file directly, then hard refresh it (CTRL+F5), and then go through the form again, it will ignore the answers I've selected and enter the data for that hard refresh I did


Solution

  • I found out how to fix this, in case anyone stumbles upon this post.

    I simple added cache:"false" to the ajax get request.