[英]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留在File Caching和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'));
}
將打印預期的和整個會話數組的key => values。 完善!!! 現在,如果我讓重定向通過。
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,就像在__destroy上內部調用的session_write_close一樣。
但是,在您發送“ Location:”標頭之后,就會發生此事件。
在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中,此問題已修復-在重定向會話組件關閉自身之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.