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 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录