CakePHP 2.6,Redis session/cache 在重定向时被销毁
CakePHP 2.6, Redis session/cache being destroyed on redirect
我正在将我们的项目从 Centos-6/Apache 2.0/PHP5.3/Cake 2.0/File Cache (6/3/2/0/F) 升级到 Centos-7/Apache 2.4/PHP5.6/Cake 2.6/Redis 缓存和会话 (7/6/4/6/R).
如果我将 7/6/4/6/R 保留为文件缓存和 php 会话,升级效果很好并且符合预期。但是我已经按照一些教程安装了 Redis,从 PHP 5.6 开始,一切都按预期工作,识别 Redis,CakePHP 在 test.php 中通过了 18 次测试,获得 18 分,但 Redis 会话正在在重定向时销毁。
Core.php
//Replaces standard
Configure::write('Session', array(
'defaults' => 'cache',
'timeout' => '100',
'start' => true,
'checkAgent' => false,
'handler' => array(
'config' => 'session'
)
));
//Engine
$engine = 'Redis';
//Bottom of Core
Cache::config ('session', array (
'Engine' => $engine,
'Prefix' => $prefix . 'cake_session_',
'Duration' => $duration
));
Bootstrap.php
Cache::config('default', array('engine' => 'Redis'));
AppController.php
public $components = array(
'Session',
'Auth' => array(
'loginRedirect'=>array('controller' => 'companies', 'action' => 'view'),
'logoutRedirect'=>array('controller' => 'users', 'action' => 'login'),
'loginAction'=>array('controller' => 'users', 'action' => 'login'),
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'password')
)
)
));
UsersController.php - 登录功能 - 来自博客示例的 C&P
if ($this->request->is('post')) {
if ($this->Auth->login()) {
//print_r($_SESSION);die();
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
将打印预期的和整个会话数组键=>值。完美的!!!现在如果我让重定向通过。
CompaniesController.php
public function view($id = null) {
print_r($_SESSION);
}
不包含键=>值。
还有两个额外的项目需要检查。修改这些后,Redis 与 Cake 2.6.4 完美配合。
1) 重新验证您的 phpinfo() 并确保没有本地会话变量阻止全局 php.ini 设置。我的来自 httpd 的 php.conf.
2) session_start() 确实需要添加,即使 CakePHP 文档声明如果使用 Session 或 Auth 的加载组件则不必使用此命令。我把命令放在 webroot 的第一行。
您需要在重定向之前调用 session_write_close,因为内部 session_write_close 调用了 __destroy。
但是这个事件发生在你发送 "Location: " header.
在 AppController 中试试这个:
public function redirect($url, $status = null, $exit = true) {
if ($exit && $this->Components->enabled('Session') && $this->Session->started()) {
session_write_close();
}
return parent::redirect($url, $status, $exit);
}
同样的问题在 Cake3 中仍然存在。在 symfony2 中,它是固定的——在重定向 session 组件自行关闭之前。
我正在将我们的项目从 Centos-6/Apache 2.0/PHP5.3/Cake 2.0/File Cache (6/3/2/0/F) 升级到 Centos-7/Apache 2.4/PHP5.6/Cake 2.6/Redis 缓存和会话 (7/6/4/6/R).
如果我将 7/6/4/6/R 保留为文件缓存和 php 会话,升级效果很好并且符合预期。但是我已经按照一些教程安装了 Redis,从 PHP 5.6 开始,一切都按预期工作,识别 Redis,CakePHP 在 test.php 中通过了 18 次测试,获得 18 分,但 Redis 会话正在在重定向时销毁。
Core.php
//Replaces standard
Configure::write('Session', array(
'defaults' => 'cache',
'timeout' => '100',
'start' => true,
'checkAgent' => false,
'handler' => array(
'config' => 'session'
)
));
//Engine
$engine = 'Redis';
//Bottom of Core
Cache::config ('session', array (
'Engine' => $engine,
'Prefix' => $prefix . 'cake_session_',
'Duration' => $duration
));
Bootstrap.php
Cache::config('default', array('engine' => 'Redis'));
AppController.php
public $components = array(
'Session',
'Auth' => array(
'loginRedirect'=>array('controller' => 'companies', 'action' => 'view'),
'logoutRedirect'=>array('controller' => 'users', 'action' => 'login'),
'loginAction'=>array('controller' => 'users', 'action' => 'login'),
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'password')
)
)
));
UsersController.php - 登录功能 - 来自博客示例的 C&P
if ($this->request->is('post')) {
if ($this->Auth->login()) {
//print_r($_SESSION);die();
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
将打印预期的和整个会话数组键=>值。完美的!!!现在如果我让重定向通过。
CompaniesController.php
public function view($id = null) {
print_r($_SESSION);
}
不包含键=>值。
还有两个额外的项目需要检查。修改这些后,Redis 与 Cake 2.6.4 完美配合。
1) 重新验证您的 phpinfo() 并确保没有本地会话变量阻止全局 php.ini 设置。我的来自 httpd 的 php.conf.
2) session_start() 确实需要添加,即使 CakePHP 文档声明如果使用 Session 或 Auth 的加载组件则不必使用此命令。我把命令放在 webroot 的第一行。
您需要在重定向之前调用 session_write_close,因为内部 session_write_close 调用了 __destroy。
但是这个事件发生在你发送 "Location: " header.
在 AppController 中试试这个:
public function redirect($url, $status = null, $exit = true) {
if ($exit && $this->Components->enabled('Session') && $this->Session->started()) {
session_write_close();
}
return parent::redirect($url, $status, $exit);
}
同样的问题在 Cake3 中仍然存在。在 symfony2 中,它是固定的——在重定向 session 组件自行关闭之前。