0

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

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

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>

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

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

public $components = array('Session');

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

Any suggestions would be greatly appreciated!


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.

Ronny vdb
  • 2,324
  • 5
  • 32
  • 74

3 Answers3

3

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"

Sadee
  • 3,010
  • 35
  • 36
1

You mentioned you have this in your view

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

Try using this

<?php print_r($this->Session->read('TakenCommand')); ?>

Also, make sure you are adding "SessionHelper".

Guillermo Mansilla
  • 3,779
  • 2
  • 29
  • 34
0

I had run into the same problems when using Session + AJAX. If you use the DebugKit.Toolbar, you will notice that even after the AJAX response the Session variable is not shown on the toolbar. I thought it was just a display problem, but could be something else. Also, I was unable to read from Session at all in my AJAX method. For your purposes that restriction may be OK, as you seem to be overwriting the Session variable rather than adding onto it.

I ended up just using a POSTback instead of an AJAX call, and the same code logic that didn't work in the AJAX call worked perfectly in the POSTback.

iso27002
  • 179
  • 7
  • After some more research I found that cakePHP resets the session on ajax requests. Some answer suggest to set checkAgent to false in core.php but this doesn't solve my problem. In CakePHP 1.x it as possible to lower the security level to solve the problem but that option is not available in 2.x – Ronny vdb Nov 08 '13 at 19:03
  • I don't know what's going on because you can still write to the Session in your AJAX function and have it be stored properly, it's just inaccessible by the View that initiated the AJAX request until the page is reloaded..in which case, you may as well do a POSTback. If you refactor from AJAX -> POSTback I'm sure your code logic will work perfectly, as was the case with me. Hopefully the POSTback behaviour isn't inexcusable for your project. – iso27002 Nov 08 '13 at 19:06