CakePHP会话无法正常工作 [英] CakePHP sessions not working

查看:57
本文介绍了CakePHP会话无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cakePHP在会话中保存数据。我已经在线阅读了文档和许多示例,但似乎无法将数据保存在会话中。

I am trying to save data in the session with cakePHP. I have read the documentation and many examples online but I can't seem to save the data in the session.

在我的控制器中,我添加了会话写入功能:

In my controller I have added the session write function:

public function take(){
    if(array_key_exists('option', $this->request->data)){
        foreach($this->request->data['option'] as $cmd => $options){
            foreach($options as $cmd => $option){
                $buildCmnd = '-1 RENDERER*'.$this->request->data['Playlist']['layer'].'*TREE*$master*SCRIPT*INSTANCE*externalCommand SET '.$this->request->data['Playlist']['el'].' '.$cmd.' text={'.$option.'}';
                $this->send($buildCmnd);
            }
        }
    }
    $this->Session->write('TakenCommand', array( $this->request->data['Playlist']['el'] ) );

    $this->autoRender = false;
}

我通过jquery ajax调用操作:

I am calling the action via jquery ajax:

$("#inputTake").click(
    function()
    {                
        jQuery.ajax({
            type:'POST',
            async: true,
            cache: false,
            url: '/Controller/take',
            success: function(response) {
                jQuery('#resultField').val(response);
            },
            data:jQuery('form').serialize()
        });
        return false;
    }
);

由于我使用的是Ajax,因此我创建了一个用来检查会话的元素。这是元素的内容:

Because I am using ajax, I created an element that I call to check the session. This is the contents of the element:

<pre>
<?php print_r($this->Session->read());?>
</pre>

但是会话变量 TakenCommand没有出现在输出中。

But the session variable "TakenCommand" does not appear in the output.

我已将Session组件添加到AppController.php文件中:

I have added the Session component to the AppController.php file:

public $components = array('Session');

我将core.php文件的会话默认值从 php更改为 cake,然后又返回了

And I have changed the core.php file session default from "php" to "cake" and back, but neither seem to help.

任何建议将不胜感激!

我见过一些答案,建议在core.php中设置'checkAgent'=> false,但这也无济于事。
可以在CakePHP 1.x中将安全级别设置为中,这可以解决问题,但是此选项在2.x中不可用。

Some answer I have seen suggest to set 'checkAgent' => false in core.php but this doesn't help either. In CakePHP 1.x is was possible to set the security level to medium which would resolve the problem, but this option is not available in 2.x.

推荐答案

如果控制器中的php open标签之前有空格或php close标签之后有空格将导致无法正常工作。关闭?> 或打开标签之前的空格,然后检查控制器(和其他组件)是否有多​​余的空间/换行;?php

If there space before the php open tag or spaces after php close tags in the controllers will cause not to work session. check your controllers (and other components if there) for extra space / new line after a closing "?> " or spaces before opening tags " <?php"

这篇关于CakePHP会话无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆