$ this-> Session-> read('Config')在cakephp中不打印任何内容

时间:2014-08-19 06:22:12

标签: php session cakephp

当我尝试打印$this->Session->read('Config')时,它什么都不打印。

我正在使用cakephp 2.5.3

我从网上搜索过,阅读过很多博客,帖子和评论并尝试解决,但我的问题仍然存在。虽然$this->Session->setFlash()工作正常,但其他会话值无效。

App::uses('Controller', 'Controller');
class AppController extends Controller {
    var $components = array('Session','RequestHandler');


    public function beforeFilter() {
        parent::beforeFilter();

        debug($this->Session->read('Config'));//null
        debug($this->Session->read('Config.userAgent'));//null
        debug($this->request->clientIp());// ::1

    }
}

1 个答案:

答案 0 :(得分:0)

 please check that at the time of login you create a session or not.

即$ this->会话 - >写('变量名','变量值')。  如果你没有这样做,你将通过你的代码得到null。  相同的代码向我提供有关我系统中登录用户的信息。  所以要确保你已经创建了一个会话, 在登录时使用一些变量名称和值组合。  请看一下: -

public function login() {

        if ($this->request->is('post') || $this->request->is('put')) {
            $user_detail = $this->User->find('first',array('conditions'=>array('username'=>$this->request->data['User']['username'], 'password'=>$this->Auth->password($this->request->data['User']['password'])),'recursive'=> -1,'fields'=>array('deleted'))); // gettin user details
            if(isset($user_detail) && !empty($user_detail)){
                if($user_detail['User']['deleted']== '0'){
                    if ($this->Auth->login()) {// checking user is authorize for login or not?
                        $this->Session->write('user_role', $this->Auth->user('role_id'));// if yes create session variable with the given variable name and value.
                        $is_incharge = $this->check_incharge($this->Auth->user('id'));
                        if ($is_incharge) {
                            $this->Session->write('is_user_incharge', true);
                        } else {
                            $this->Session->write('is_user_incharge', false);
                        }
                        $this->redirect($this->Auth->loginRedirect);
                    } else {
                        $this->Session->setFlash(__('Invalid User name or Password.'));
                        $this->redirect($this->Auth->logoutRedirect);
                    }
                }else{
                    $this->Session->setFlash(__('You are deleted.Can not login.'));
                    $this->redirect($this->Auth->logoutRedirect);
                }
            }else{
                $this->Session->setFlash(__('Invalid User name or Password..'));
                $this->redirect($this->Auth->logoutRedirect);
            }
        }
    }