CakePHP 2.6,Redis会话/缓存在重定向时被销毁

时间:2015-04-27 19:46:01

标签: php session cakephp caching redis

我正在将项目从Centos-6 / Apache 2.0 / PHP5.3 / Cake 2.0 /文件缓存(6/3/2/0 / F)升级到Centos-7 / Apache 2.4 / PHP5 .6 / Cake 2.6 / Redis Cache&会议(7/6/4/6 / R)。

如果我使用文件缓存和php会话离开7/6/4/6 / R,升级可以很好地工作。但是我已经按照一些教程安装了Redis,并且所有内容都按照PHP 5.6的标准运行,识别Redis,CakePHP在test.php中获得18次测试,但Redis Sessions在Redirects上被销毁。

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'd

    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'));
}

将打印预期和整个Session Array key =>值。完善!!!现在,如果我让重定向通过。

CompaniesController.php

public function view($id = null) {
        print_r($_SESSION);
}

不包含key =>值。

1 个答案:

答案 0 :(得分:0)

你需要在重定向之前调用session_write_close,因为在__destroy上调用了内部session_write_close。

但是这个事件发生在你发送"位置:"报头中。

在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中,它已修复 - 在重定向会话组件关闭之前。

相关问题