我正在使用MVC pattren并在Controller类的构造中启动会话。我的类(期望内核的类)由spl_autoload_register()加载。我创建一个AJAX类作为Controller的子类,以防止用户在短时间内执行多个Ajax请求,我使用以下代码
class ajax extends Controller
{
public function __construct()
{
parent::__construct(false);
if ($this->checkAttempts())
exit();
}
public function checkAttempts()
{
$counter = 0;
$attempts = Session::get(AJAX);
$attempts = ($attempts === false) ? [] : $attempts;
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());
}
//ajax methods ...
}
问题是当我打开另一个控制器(例如home)(没有构造)时
class home extends Controller {
public function index(){...}
}
该脚本在ajax类的checkAttepmt方法中初始化会话,这意味着它调用了方法checkAttepmt,并且仅设置了会话,例如,当我回声什么都没出现时!
当我在Home控制器中打印会话变量时,我发现ajax控制器中设置的会话存在(我在每次测试中都将其清除)。
我像这样使用debug_backtrace()来解决问题
class ajax extends Controller
{
public function __construct()
{
parent::__construct(false);
$_SESSION['trace'] = debug_backtrace();
}
}
结果是
[trace] => Array
(
[0] => Array
(
[file] => D:\__path__\application\core\application.php
[line] => 24
[function] => __construct
[class] => ajax
[object] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => ajax
)
[type] => ->
[args] => Array
(
)
)
[1] => Array
(
[file] => D:\__path__\public\index.php
[line] => 15
[function] => __construct
[class] => Application
[object] => Application Object
(
[controller:protected] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => ajax
)
[method:protected] => getConfirmMsg
[params:protected] => Array
(
)
)
[type] => ->
[args] => Array
(
)
)
__PHP_Incomplete_Class和此问题之间有什么关系,以及如何解决?
我发现了问题!在单击事件发生之前触发了一些jQuery ajax请求,我现在修复了它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句