会话 cookie 不是用 CakePHP 2 编写的


Session cookie not being written in CakePHP 2

上周末,我试图解决一个网站上没有保留会话的错误 - 今天我在我的笔记本电脑上做了进一步的工作,我无法再登录 - 我总是做了一些非常愚蠢的事情。

我在Windows笔记本电脑上使用xampp,并在本地主机上工作,这发生在所有浏览器中。 我在解决此类问题方面不是很有经验 - 我已经能够确定以下内容:

  • 用户能够登录(Auth->login(( 成功登录用户(,问题是重定向时会话已消失
  • 我可以看到会话正在我的/tmp/dir 中写入,其中包含(看起来(正确的数据
  • 我可以创建自己的愚蠢饼干,并且它们的价值仍然存在
  • 该网站不存在其他 Cookie

因此,在我看来,会话cookie没有被设置,但是我已经对为什么会发生这种情况没有想法。 我没有更改任何与 Cookie 相关的浏览器设置(除了在 IE 中启用 Cookie 之外(,并且我已经仔细检查了我的 Chrome cookie 设置。 正如我所提到的,我还在AppController中编写了一些垃圾cookie,我可以看到它们被创建,并且它们的数据仍然存在。

如果我在 login(( 后调用 $_SESSION,一切看起来都很棒,但如果我在登录前打印 $_SESSION,它是空的。

我很确定我已经设法做了一些迟钝的事情,但我已经没有关于它可能是什么的想法。 我已经将我的/app/core.php 恢复为蛋糕默认值:

    Configure::write('Session', array(
    'defaults' => 'php'
));

我的 login(( 函数基本上如下所示:

    public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());        
        } else { 
            $this->Session->setFlash(__('Invalid username or password, try again.')); 
        }

应用控制器中的身份验证设置:

class AppController extends Controller {
public $components = array(
    'Session',
    'Cookie',
    'Acl',
    'Email',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email', 'password' => 'password')
        )),
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        ),
        'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
    ),
);

以及在 login(( 中重定向之前打印 $this->Auth->user((, $_SESSION 的示例输出:

'app'Controller'UsersController.php (line 203)
array(
    'id' => '10',
    'name' => 'super',
    'is_active' => '1',
    'email' => 'super@test.com',
    'group_id' => '3',
    'address' => '3',
    'phone' => 'xxxxx',
    'category' => 'P',
    'communication_in' => 'E',
    'created' => '2014-11-29 16:27:19',
    'modified' => '2014-11-29 16:27:19',
    'Group' => array(
        'id' => '3',
        'name' => 'Administrators',
        'created' => '2014-11-16 21:01:35',
        'modified' => '2014-11-16 21:01:35'
    )
)
'app'Controller'UsersController.php (line 204)
array(
    'Config' => array(
        'userAgent' => '4af162a3a94462226b6e93c6806203aa',
        'time' => (int) 1417317929,
        'countdown' => (int) 10,
        'language' => 'eng'
    ),
    'Auth' => array(
        'User' => array(
            'id' => '10',
            'name' => 'super',
            'is_active' => '1',
            'email' => 'super@test.com',
            'group_id' => '3',
            'address' => '3',
            'phone' => 'xxxx',
            'category' => 'P',
            'communication_in' => 'E',
            'created' => '2014-11-29 16:27:19',
            'modified' => '2014-11-29 16:27:19',
            'Group' => array(
                'id' => '3',
                'name' => 'Administrators',
                'created' => '2014-11-16 21:01:35',
                'modified' => '2014-11-16 21:01:35'
            )
        )
    )
)

上次创建的会话文件:

Config|a:4:{s:9:"userAgent";s:

32:"4af162a3a94462226b6e93c6806203aa";s:4:"time";i:1417317929;s:9:"countdown";i:10;s:8:"language";s:3:"eng";}Auth|a:1:{s:4:"User";a:12:{s:2:"id";s:2:"10";s:4:"name";s:5:"super";s:9:"is_active";s:1:"1";s:5:"email";s:14:"super@test.com";s:8:"group_id";s:1:"3";s:7:"address";s:1:"3";s:5:"phone";s:10:"xxxxx";s:8:"category";s:1:"P";s:16:"communication_in";s:1:"E";s:7:"created";s:19:"2014-11-29 16:27:19";s:8:"modified";s:19:"2014-11-29 16:27:19";s:5:"Group";a:4:{s:2:"id";s:1:"3";s:4:"name";s:14:"Administrators

";s:7:"created";s:19:"2014-11-16 21:01:35";s:8:"modified";s:19:"2014-11-16 21:01:35";}}}

今日掌:几个小时后,我终于想到检查phpinfo((,当然,session.cookie-domain被设置为远程站点。 我想在上周的某个时候我编辑了错误的PHP ini文件。