duannuochi3549 2016-07-15 13:58
浏览 44
已采纳

如何在会话到期后修复Zend DbTableGateway读取/销毁无限循环?

I have a Zend\Session Manager with a DbTableGateway to handle sessions. My code is in another SO answer. But whenever the session expires, I get an error Fatal error: Maximum function nesting level of '100' reached, aborting! in [path]\vendor\zendframework\zend-db\src\Sql\AbstractExpression.php on line 40

I found out how to change the xdebug nesting level to 200, but then I reached the 200 limit too.

Picture of the fatal error.

So I have an infinite loop between DbTableGateway->read() and DbTableGateway->destroy(). I noticed the 2 lines referenced in the error each call the other method, but it's Zend code so I don't want to change it.

It only happens after the session expires, and not when I remove the row containing the session information from the database. When I looked at the table after this occurred, I noticed that the row is still present in the table except for the data column.

id              name            modified    lifetime data
[random string] [session name]  1468587768  1440     -

So it looks like the read() method checks the table for a row containing the id and name, and finds this row, then calls destroy(), which calls read(). How can I fix this?

On a side note, refreshing the page solves the problem for the user, but I don't want to have code that throws an exception at the user and forces them to refresh the page.

  • 写回答

1条回答 默认 最新

  • du3669 2016-07-15 20:52
    关注

    This worked for me: create a class that extends DbTableGateway and modify its destroy method to check if a row exists rather than call the read() function. Then use that class instead of DbTableGateway.

    So here's the code for the new save handler class:

    class MySessionSaveHandler extends DbTableGateway
    {
        /**
         * Destroy session
         *
         * @param  string $id
         * @return bool
         */
        public function destroy($id)
        {
            // I removed "read" to prevent an infinite loop, and am using the first line of "read" instead to check if the row exists
            $rows = $this->tableGateway->select([
                $this->options->getIdColumn()   => $id,
                $this->options->getNameColumn() => $this->sessionName,
            ]);
    
            if (! (bool) $rows) {
                return true;
            }
    
            return (bool) $this->tableGateway->delete([
                $this->options->getIdColumn()   => $id,
                $this->options->getNameColumn() => $this->sessionName,
            ]);
        }
    }
    

    And here's how you replace DbTableGateway:

    /* @var $adapter \Zend\Db\Adapter\Adapter */
    $adapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
    $tableGateway = new TableGateway('mytablename', $adapter);
    return new MySessionSaveHandler($tableGateway, new DbTableGatewayOptions());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题