Reputation: 348
i was just wondering if a SESSION variable can be sometimes slow to set/unset?
To be honest I'm not sure how to test exactly how quick a SESSION becomes set or unset but i have noticed when i use an ajax call to set/unset a SESSION variable about 10% of the time the code in the ajax callback function executes before the SESSION variable is set/unset.
I have put a setTimeout in the callback function to have it wait a second before it executes to code and this seems eliminate the problem, though it kind of seems like a bad fix. Is it considered bad practice to do something like that?
Learner here, be kind :-)
jQuery
$("#whatever_button").click(function(){
var $x = "1";
$.ajax({
url: "filename.php",
data: { set_session: $x},
type: "post",
dataType: "text",
success: function(response){
if (response == "1"){
//delay execution for 1 second
setTimeout(function(){
//Place code here
}, 1000);
} else alert("Something went wrong.");
},
});
});
PHP
if (isset($_POST['set_session'])){
unset($_SESSION['variable']); //unset session variable
echo "1";
}
Upvotes: 1
Views: 1432
Reputation: 73251
A session is stored in a file, so depending on your server it can take some milliseconds to save it, though I would hardly doubt that this will ever be noticeable unless you have set your session_save_path to a 1985' floppy drive with a barely working floppy in it.
More interesting is the purpose you are aiming for, as you don't return anything related to the newly unset $_SESSION['variable']
.
Maybe you should include a test to see if the session is unset
unset($_SESSION['variable']);
if ( ! isset($_SESSION['variable'])) return '1';
If you want to check times, you can allways make use of php's microtime()
function.
$time_start = microtime(true);
unset($_SESSION['variable']);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "$time seconds";
Upvotes: 1