i have a jQuery ajax call with this parameters :
url : "ajax/_method_",
type : 'POST' ,
data : _data_,
dataType : 'JSON',
async: true,
in my ajax controller i make a setAttempt & checkAttempts functions to prevent users from multiple ajax requests this is my code :
public function checkAttempts()
{
$counter = 0;
$attempts = Session::get(AJAX);
$attempts = ($attempts === false) ? [] : $attempts;
//when user make his first request => there's no ajax requests Session
if ($attempts === false)
Session::set(AJAX, []);
else
$this->setAttempt();
foreach ($attempts as $key => $attempt)
{
if (time() - intval($attempt) <= 10)
$counter++;
else
Session::destroyArray(AJAX, $key);
}
return $counter >= 5;
}
public function setAttempt()
{
return Session::setArray(AJAX, time());
}
this code works fine if i acceed directly to ' ajax/method but when i use the jquery ajax request utility i face a problem which when the user make his first request the session initialized to :
array (size=3)
'AJAX_ATTEMPTS' =>
array (size=4)
0 => int _timestamp_
1 => int _timestamp_
2 => int _timestamp_
3 => int _timestamp2_
if found that the method ajax/checkAttempts is called several times when i open another controller such ( home - store ... ) and this shoudln't happen because the method declared only in the ajax controller and called only in the ajax __constructor
public function __construct()
{
parent::__construct(false);
if ($this->checkAttempts())
{
exit();
}
}
by using the debug_backtrace() function i found that the application core call the ajax constructor when it initialize the controller class! how to prevent this issue ?