dongyou5068 2015-08-30 19:14
浏览 49
已采纳

防止在不完整的php对象中初始化会话

i'm using MVC pattren and start the sessions in the construct of the Controller class . my class (expect core's one) are loaded by spl_autoload_register(). i create an AJAX class that's a subclass of the Controller in order to prevent user from executing multiple ajax request in a short interval of time i use the following code

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 ...
 }

the problem is when i open another Controller such as home (haven't a construct)

class home extends Controller {
    public function index(){...}
}

the script initialize the sessions in the checkAttepmt method of the ajax class it mean the method checkAttepmt it called and only the session are set for eg when i echo something nothing appear !

when i print the session variable in Home controller i found the session set in the ajax controller exists ( i clear them in each test ) .

i used debug_backtrace() like this to figure out the problem

class ajax extends Controller
{

    public function __construct()
    {
        parent::__construct(false);
        $_SESSION['trace'] = debug_backtrace();
    }
}

the result is

[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
                        (
                        )

                )

what's the relation between the __PHP_Incomplete_Class and this problem and how to slove it ?

  • 写回答

1条回答 默认 最新

  • dongyan1841 2015-08-30 21:54
    关注

    i found out the problem ! there's some jQuery ajax requersts fired before the click event happen i fix it now .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路