【发布时间】:2015-11-24 20:04:39
【问题描述】:
我正在使用 MVC 模式并在 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 和这个问题有什么关系,如何解决?
【问题讨论】: