I am programming a website that use jQuery's asynchrony to call php scripts. To avoid double script calling I would like to do two things:
- disable the buttons that will trigger the script call, which I have successfully done
- set a timer in the php script that is called that will check whether it was called by the same user less than one second ago, and if so, exits
First I thought this would be relatively easy, and came up with the following code:
if(!isset($_SESSION['time'])) $_SESSION['time']=time();
else if(time()-$_SESSION['time']<=1){
//If less than one second ago, timer reset
$_SESSION['time']=time();
exit;
}
It works fine the first time, but then afterwards, when it should check again, of course a very old time has been saved in the session and is still in there, so the script can be called as often as the user wishes.
Do you have any ideas how to solve this? Thank you!
Charles
EDIT
My question could possibly be a duplicate of this - do you see any value in the second answer maybe?
SOLUTION FOUND
This is the solution:
if(!isset($_SESSION['time']) || ($_SESSION['time'] + 1 < time())){
$_SESSION['time'] = time();
}else if(time()-$_SESSION['time']<=1){
//Timer reset
$_SESSION['time']=time();
exit;
}