cakephp lost session variable when redirect

后端 未结 5 1011
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 04:48

I have problems with a session variable, users log into the app and then it sets a session variable but when it redirects to the next controller it isn\'t there.

At

相关标签:
5条回答
  • 2020-12-17 05:03

    I have the same problem. I tried all the suggestion. My Cache engine is Apc.

      $this->__saveData($t);
      debug($this->Session->read());// >>>>>> GOOD
      $this->redirect(array('controller'=>'users','action'=>'main'));
        }
    }
     }
      function logout() {
        $this->Session->destroy();
                $this->Session->delete('User');
        $this->redirect(array('controller'=>'logins','action'=>'login'));
    }
    function forgot() {
    $this->layout = 'login';
    
    } 
        private function __saveData($t)
        {   
             $this->Session->write('User',$t['User']['name']);
             $this->Session->write('User_name',$t['User']['firstname']);
             $this->Session->write('User_id',$t['User']['id']);
             $this->Session->write("User_Group",$t['Group']['name']);
             $g = $this->Myauth->getPerm('User_Group'); // This is the array of permission w.r.t to the menu (key)
             $this->Session->write("Permissions",$g);
    
             debug($this->Session->read());
        }
     function main()
    {
        // Check permissions
    $this->Myauth->check('users','login');
    $username   = $this->Session->read('User');
    debug($this->Session->read( ));die(); <<<<< NOTHING
    

    }

    The funny thing is that yesterday it worked.

    My php.ini has a simple extension=apc.so. My core.php

        Configure::write('Session.defaults', 'php');
    

    Nothing change if I change the Security level. I will appreciate any direction.

    EDIT First solution: in my php.ini I had a bad value for session.referer_check (It was = 0 while it should be ''). But now, on the same server, one site is ok. Another one fires the error Error: Call to undefined function apc_cache_info()

    The two sites are separated and do not share any cakelib.

    [SOLUTION FOUND]

    For Cake > 2.2 and Chrome 24 I found this solution (I tried all the others found on the web). In your core.php:

         Configure::write('Security.cookie', 'cakephpfdebackend');
         Configure::write('Session.cookieTimeout', 0);
         Configure::write('Session.checkAgent', false);
         Configure::write('Session.cookie_secure',false);
         Configure::write('Session.referer_check' ,false);
         Configure::write('Session.defaults', 'php'); 
    

    Actually, only the Session.cookieTimeout is required. The other settings are optional to solve the problem.

    0 讨论(0)
  • 2020-12-17 05:07

    A possible reason for this problem is that the server clock is not synced with the client's clock and thus the cookie timeouts.

    0 讨论(0)
  • 2020-12-17 05:17

    I had some issue with session on some pages . Can you check whether any space comes at the bottom of page after the php ending tag. When i faced this problem, i found session is missing due to a white space character in controller after the php ending tag . Please check this and let me know .

    0 讨论(0)
  • 2020-12-17 05:22

    You are overriding the beforeFilter() method. So, instead of using this:

    <?php
    class UsuariosController extends AppController {
    
        function beforeFilter() {
    
        }
    

    you should do this:

    <?php
    class UsuariosController extends AppController {
    
        function beforeFilter() {
            parent::beforeFilter();
        }
    
    0 讨论(0)
  • 2020-12-17 05:25

    I was losing session information after a login call too and after searching for a while I found many different ways to fix my issue. I only regret that I don't fully understand what is causing the issue, but I imagine it has to do with php's session configuration.

    1. As you mentioned, changing Security.level to low fixed the issue for me Configure::write('Security.level', 'low');
    2. Changing the session save configuration to php fixed the issue for me too: Configure::write('Session', array( 'defaults'=>'cake', ));
    3. And finally adding a timeout worked too (which I ended up using): Configure::write('Session', array( 'defaults'=>'php', 'cookieTimeout'=> 10000 ));

    All these found in /app/Config/core.php

    I post this hoping someone is able to make sense of what is going on underneath. I feel understanding the root of the issue would make a better job of answering your question.

    0 讨论(0)
提交回复
热议问题