[英]Zf2 Db Sessions Inserts but doesn't update sessions in database
我已經在zf2(骨架)應用程序中配置了數據庫會話,以使用文檔中所述的典型MySQL數據庫。 但是,該應用程序僅插入會話,而從不更新特定行。 因此,每次刷新都會創建一個新會話,並且以前的信息將無法檢索。
我的配置如下:
global.php
-------------------
'session_config' => array
(
'cache_expire' => 2419200,
'cookie_lifetime' => 2419200,
'gc_maxlifetime' => 2419200,
'cookie_path' => '/',
'cookie_secure' => TRUE,
'remember_me_seconds' => 2419200,
'use_cookies' => true,
)
我的會話表創建如下(數據庫用戶具有選擇,插入和更新privs):
CREATE TABLE `sessions`
(
`id` char(32) NOT NULL DEFAULT '',
`name` varchar(255) NOT NULL,
`modified` int(11) DEFAULT NULL,
`lifetime` int(11) DEFAULT NULL,
`data` text,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
我的數據庫會話在Application / Module.php的工廠關閉中配置:
public function getServiceConfig()
{
return array
(
'factories' => array
(
// Sessions
'session_manager' => function($serviceManager)
{
$sessionOptions = new DbTableGatewayOptions();
$sessionOptions->setDataColumn('data')
->setIdColumn('id')
->setModifiedColumn('modified')
->setLifetimeColumn('lifetime')
->setNameColumn('name');
$dbAdapter = $serviceManager->get('utils-db');
$sessionTableGateway = new TableGateway('sessions', $dbAdapter);
$sessionGateway = new DbTableGateway
($sessionTableGateway, $sessionOptions);
$config = $serviceManager->get('config');
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config['session_config']);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->setSaveHandler($sessionGateway);
return $sessionManager;
},
...
...
),
);
}
會話通過onBootstrap方法啟動:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$serviceManager = $e->getApplication()->getServiceManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$sessionManager = $serviceManager->get('session_manager');
$sessionManager->start();
Container::setDefaultManager($sessionManager);
...
}
我可以從概念上理解zf2可以在會話中寫入/插入會話,但是它要么無法選擇特定行,要么可以找到特定行而只是無法更新它。 因此,我鑽入Zend \\ Session \\ SaveHandler並在DbTableGateway類中找到了它:
/**
* Write session data
*
* @param string $id
* @param string $data
* @return bool
*/
public function write($id, $data)
{
$data = array(
$this->options->getModifiedColumn() => time(),
$this->options->getDataColumn() => (string) $data,
);
$rows = $this->tableGateway->select(array(
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
));
if ($row = $rows->current()) {
return (bool) $this->tableGateway->update($data, array(
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
));
}
$data[$this->options->getLifetimeColumn()] = $this->lifetime;
$data[$this->options->getIdColumn()] = $id;
$data[$this->options->getNameColumn()] = $this->sessionName;
return (bool) $this->tableGateway->insert($data);
}
顯然,根據條件,它將更新為true,如果不是,則在下面進行插入:
if ($row = $rows->current()) {
return (bool) $this->tableGateway->update($data, array(
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
));
}
對我來說很奇怪
if ($row = $rows->current()) {
變量$ row似乎沒有設置在上面,但是$ rows確實以正確的select語句結尾:
SELECT `sessions`.* FROM `sessions` WHERE `id` = :where1 AND `name` = :where2
id is set correctly
name is set correctly
我只是不知道為什么我不能更新自己的觀點。 順便說一句,我可以從應用程序中的會話中讀取數據。 但是,一旦頁面刷新,所有數據就消失了。
如果您不是僅通過HTTPS運行應用程序,則應將會話配置選項cookie_secure
為false
(或忽略它-默認為false
)。 cookie_secure
設置為true
,將僅通過HTTPS發送cookie。
在HTTP請求上,這將導致每個請求都創建一個新的會話,因為請求中不存在會話cookie。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.