如何在使用Zend \ Session时设置会话cookie_domain

时间:2016-02-06 09:53:59

标签: php zend-framework

这是我的代码,我遵循了Zend \ Session文档的基本示例:

<?php

require_once 'vendor/autoload.php';

use Zend\Session\Container;
use Zend\Session\SessionManager;
use Zend\Session\Config\StandardConfig;

$config = new StandardConfig();
$config->setOptions(array(
    //'remember_me_seconds' => 1800,
    //'name'                => 'zf2',
    'cookie_domain' => '.jt.martyndev',
));
$manager = new SessionManager($config);
Container::setDefaultManager($manager);

$container = new Container('namespace');
$container->item = 'foo';

?>
<pre><?php var_dump($_SESSION); ?></pre>

我可以看到我的数据写入会话但是firebug中的PHPSESSID显示cookie仍然包含完整的域(使用子域 - dom1.jt.martyndev,而不是.jt.martyndev)

1 个答案:

答案 0 :(得分:1)

好的,看来Zend的文档有点误导? http://framework.zend.com/manual/current/en/modules/zend.session.config.html在StandardConfig下列出了“name”,“cookie_domain”。但是,此类中应该设置INI的实现是:

/**
* Set storage option in backend configuration store
*
* Does nothing in this implementation; others might use it to set things
* such as INI settings.
*
* @param  string $storageName
* @param  mixed $storageValue
* @return StandardConfig
*/
public function setStorageOption($storageName, $storageValue)
{
   return $this;
}

/**
* Retrieve a storage option from a backend configuration store
*
* Used to retrieve default values from a backend configuration store.
*
* @param  string $storageOption
* @return mixed
*/
public function getStorageOption($storageOption)
{
   return;
}

相反,我将代码更改为SessionConfig,我可以在检查器中看到cookie域设置为我想要的:

use Zend\Session\Config\SessionConfig;
$config = new SessionConfig();
.
.