为什么我不能在CodeIgniter钩子中使用会话 [英] Why I can't use sessions in a CodeIgniter hook
本文介绍了为什么我不能在CodeIgniter钩子中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这里的代码:
hooks / account.php:
class Account {
public function checkIfLogged(){
if(!$ this-> session-> userdata('logged')){
$ this-> load-& error / not_found');
exit;
}
}
}
未定义的属性:Account :: $ session
我可以确认我的钩子是一个post_controller_constructor。
有人可以告诉我我错了什么?
感谢。
$ b
解决方案
p> $ this-> CI =& get_instance();
if(!$ this-> CI-> session-> userdata('logged')){
$ this-> CI-> load-&未找到');
exit;
}
这只是一个范围问题,这就是为什么你在这里使用CI。 / p>
I have this code here: hooks/account.php:
class Account {
public function checkIfLogged() {
if(!$this->session->userdata('logged') ){
$this->load->view('error/not_found');
exit;
}
}
}
and I get this error:
Undefined property: Account::$session
I can confirm that my hook is a post_controller_constructor.
Can somebody tell me where I am going wrong?
Thanks.
解决方案
you should use:
$this->CI = & get_instance();
if(!$this->CI->session->userdata('logged') ){
$this->CI->load->view('error/not_found');
exit;
}
It's just a matter of scope that's why you use CI here this way.
这篇关于为什么我不能在CodeIgniter钩子中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文