Javascript php会话中的Jquery ajax问题
我使用以下参数调用了jQuery ajax:Javascript php会话中的Jquery ajax问题,javascript,php,jquery,ajax,session,Javascript,Php,Jquery,Ajax,Session,我使用以下参数调用了jQuery ajax: url : "ajax/_method_", type : 'POST' , data : _data_, dataType : 'JSON', async: true, 在我的ajax控制器中,我使用settrunt&checktrunts函数来阻止用户执行多个ajax请求 这是我的代码: public function checkAttempts() { $counter = 0; $at
url : "ajax/_method_",
type : 'POST' ,
data : _data_,
dataType : 'JSON',
async: true,
在我的ajax控制器中,我使用settrunt&checktrunts函数来阻止用户执行多个ajax请求
这是我的代码:
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());
}
如果发现在我打开另一个控制器(如home-store…)时多次调用了方法ajax/checkAttempts,那么这不应该发生,因为该方法仅在ajax控制器中声明,并且仅在ajax构造函数中调用
public function __construct()
{
parent::__construct(false);
if ($this->checkAttempts())
{
exit();
}
}
通过使用debug_backtrace()函数,我发现应用程序核心在初始化控制器类时调用ajax构造函数!
如何防止此问题?我想这是因为您直接调用了ajax/method,然后尝试使用ajax调用它,但会话已经初始化。我在使用直接调用后清除了此会话,但是我发现了一些新的东西,ajax/checkAttempts方法在打开任何其他控制器时调用,但这不应该发生,因为这个方法只在ajax控制器中!!只能在接收到ajax调用时调用方法吗?我认为,阻止初始化控制器类是错误的。类应该正确初始化。我只在ajax类构造中调用此方法,但在初始化父类控制器时调用子类的此构造,这就是问题所在!
public function __construct()
{
parent::__construct(false);
if ($this->checkAttempts())
{
exit();
}
}