在数据库中存储会话 - 数据被覆盖

时间:2012-12-30 22:01:22

标签: php session

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

我最近实施了使用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();
}

我在这里遗漏了一些导致空会话覆盖数据库中活动会话的内容吗?

由于

0 个答案:

没有答案