【问题标题】:CakePHP 2.6, Redis session/cache being destroyed on redirectCakePHP 2.6,Redis 会话/缓存在重定向时被破坏
【发布时间】:2015-07-06 10:18:29
【问题描述】:

我正在将我们的项目从 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'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'));
}

将打印预期的和整个会话数组键=>值。完美的!!!现在,如果我让重定向通过。

CompaniesController.php

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

不包含键=>值。

【问题讨论】:

    标签: php session cakephp caching redis


    【解决方案1】:

    有两个额外的项目需要检查。修改后,Redis 与 Cake 2.6.4 完美兼容。

    1) 重新验证您的 phpinfo() 并确保没有本地会话变量阻止全局 php.ini 设置。我的来自 httpd 的 php.conf。

    2) session_start() 确实需要添加,即使 CakePHP 文档声明如果使用加载的 Session 或 Auth 组件,您不必使用此命令。我将命令放在 webroot 的第一行。

    【讨论】:

      【解决方案2】:

      您需要在重定向之前调用 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 中它是固定的——在重定向会话组件关闭之前。

      【讨论】:

        猜你喜欢
        • 2012-08-20
        • 2012-10-31
        • 1970-01-01
        • 2019-04-17
        • 1970-01-01
        • 2019-07-01
        • 2012-12-15
        • 2013-01-29
        • 2023-03-04
        相关资源
        最近更新 更多