【问题标题】:$this->Session->read('Config') prints nothing in cakephp$this->Session->read('Config') 在 cakephp 中不打印任何内容
【发布时间】:2014-08-19 06:22:12
【问题描述】:

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

我使用的是 cakephp 2.5.3

我从网上搜索,阅读了许多博客、帖子和 cmets 并试图解决它,但我的问题仍然存在。虽然 $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

    }
}

【问题讨论】:

  • 会不会是$this->session,下面是s
  • CakePHP 2.9 不存在。目前最新的 2.x 版本是 2.5.3 :-)
  • 如果你尝试debug($this->Session->read());会得到什么?
  • 打印行号和空
  • 你在给 Session 写什么吗?也许你想要Configure::read()

标签: php session cakephp


【解决方案1】:
 please check that at the time of login you create a session or not.

即$this->Session->write('变量名','变量值')。 如果您不这样做,您将通过您的代码获得 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);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多