简体   繁体   English

CakePHP 2.6,Redis会话/缓存在重定向时被破坏

[英]CakePHP 2.6, Redis session/cache being destroyed on redirect

I'm in the process of upgrading our project from Centos-6/Apache 2.0/PHP5.3/Cake 2.0/File Cache (6/3/2/0/F) to Centos-7/Apache 2.4/PHP5.6/Cake 2.6/Redis Cache & Session (7/6/4/6/R). 我正在将我们的项目从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)。

The upgrade works great and as intended if I leave the 7/6/4/6/R with File Caching and php sessions. 如果我将7/6/4/6 / R留在File Caching和php会话中,则升级效果很好,符合预期。 But I've installed Redis following a few tutorials and everything works as intended from PHP 5.6 recognizing Redis, CakePHP gets 18 for 18 tests passes in test.php, yet the Redis Sessions are being destroyed on Redirects. 但是我已经按照一些教程安装了Redis,并且所有功能都可以从PHP 5.6识别Redis的目的开始,CakePHP在test.php中获得18个测试通过18次,但是Redis会话在重定向上被破坏了。

Core.php 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 Bootstrap.php

Cache::config('default', array('engine' => 'Redis'));

AppController.php 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 - Login Function - C&P'd from Blog example 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'));
}

Will print the intended and entire Session Array key=>values. 将打印预期的和整个会话数组的key => values。 PERFECT!!! 完善!!! Now If I let the redirect through. 现在,如果我让重定向通过。

CompaniesController.php CompaniesController.php

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

Contains no key=>values. 不包含键=>值。

There are two extra item to check. 有两个额外的项目要检查。 After these were modified Redis worked perfectly with Cake 2.6.4. 修改这些内容后,Redis与Cake 2.6.4完美配合。

1) Re-verify your phpinfo() and make sure there is no local session variables blocking the Global php.ini settings. 1)重新验证您的phpinfo()并确保没有阻止全局php.ini设置的本地会话变量。 Mine were coming from httpd's php.conf. 我的人来自httpd的php.conf。

2) session_start() does need to be added, even though CakePHP Documentation states you don't have to use this command if the loaded component of Session or Auth is used. 2)确实需要添加session_start(),即使CakePHP文档指出如果使用了Session或Auth的已加载组件,您也不必使用此命令。 i placed the command on line one of webroot. 我将命令放在webroot的第一行。

You need to call session_write_close before redirect as internally session_write_close called on __destroy. 您需要在重定向之前调用session_write_close,就像在__destroy上内部调用的session_write_close一样。

But this event happens after you have sent "Location: " header. 但是,在您发送“ Location:”标头之后,就会发生此事件。

Try this in AppController: 在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);
}

Same problem still in Cake3. Cake3中仍然存在相同的问题。 In symfony2 it's fixed — before redirect session component closes itself. 在symfony2中,此问题已修复-在重定向会话组件关闭自身之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM