1

某处,某事导致将空会话写入我的数据库。

我最近实施了使用 Zend/Db 的更改,只是在此之后我遇到了问题。

这是我的阅读功能

public function read($sessionId)
{

    $sql = new Sql($this->database);
    $predicate = new Where();

    $query = $sql->select();
    $query->from('sessions');
    $query->columns(array('sessiondata'));
    $query->where($predicate->equalTo('sessionid', $sessionId));
    $query->where($predicate->greaterThan('expiry', new Expression("NOW()")));

    /**
     * @var $stmt \Zend\Db\Adapter\Driver\StatementInterface;
     */

    $stmt = $sql->prepareStatementForSqlObject($query);
    $result = $stmt->execute();


    if ($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows() > 0) {
        $resultSet = new ResultSet;
        $resultSet->initialize($result);

        $data = $resultSet->toArray();
        return $data[0]['sessiondata'];
    } else {
        echo "read_fail";
        return false;
    }
}

还有我的写功能

public function write($sessionId, $sessionData)
{

    $expiry = date("Y-m-d H:i:s", time() + CATALINA_MAX_SESSION_TIME - 1);


    $sql = new Sql($this->database);
    $query = $sql->select()
        ->from('sessions')
        ->columns(
            array(
                'sessionid'
            )
        )
        ->where(
            array(
                'sessionid' => $sessionId
            )
        );

    /**
     * @var $stmt \Zend\Db\Adapter\Driver\StatementInterface;
     */

    $stmt = $sql->prepareStatementForSqlObject($query);
    $result = $stmt->execute();

    $sql = new Sql($this->database);

    if ($result->count() > 0) {

        $query = $sql->update('sessions')
            ->set(array(
                        'expiry' => new Expression("NOW() + interval 1 hour"),
                        'touch' => new Expression("NOW()"),
                        'sessiondata' => $sessionData
                )
        )
            ->where(array('sessionid' => $sessionId));

    } else {

        $query = $sql->insert()
            ->into('sessions')
            ->columns(
                array(
                    'sessionid',
                    'expiry',
                    'sessiondata'
                )
            )
            ->values(
                array(
                    'sessionid' => $sessionId,
                    'expiry' => $expiry,
                    'sessiondata'=> $sessionData
                )
            );
    }

    $stmt = $sql->prepareStatementForSqlObject($query);
    $result = $stmt->execute();
}

我在这里遗漏了什么导致空会话覆盖数据库中的活动会话吗?

谢谢

4

0 回答 0