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 .

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

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)