当我尝试打印时$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
}
}
please check that at the time of login you create a session or not.
那是$ this-> Session-> write('variable name','variable value')。如果您不这样做,那么您的代码将为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);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句