php 如何在 CodeIgniter 的库中使用会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12344653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use session in a library in CodeIgniter?
提问by Chandara Sam
I want to check if user is logged in CodeIgniter by using my library in the controller's constructor.
我想通过在控制器的构造函数中使用我的库来检查用户是否已登录 CodeIgniter。
This is my library:
这是我的图书馆:
class Administrator_libs {
public function validate_authen(){
if( $this->session->userdata('user_authen') ){
redirect(base_url().'admin/login/');
}
}
}
And this is my controller:
这是我的控制器:
class Administrator extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('administrator_libs');
$this->administrator_libs->validate_authen();
$this->load->model('mod_menu');
}
}
But I get this error message:
但我收到此错误消息:
Undefined property: Administrator_libs::$session
How can I use session in a library in CodeIgniter?
如何在 CodeIgniter 的库中使用会话?
回答by Jordan Arseno
If you want to access any CodeIgniter library inside of your own, you must call get_instance(). This is because $thisis bound to your current library and not the CodeIgniter object.
如果要访问自己内部的任何 CodeIgniter 库,则必须调用get_instance(). 这是因为$this绑定到您当前的库而不是 CodeIgniter 对象。
$CI =& get_instance();
if( $CI->session->userdata('user_authen') ){
redirect(base_url().'admin/login/');
}
Please see Creating LibrariesCodeIgniter Documentation. Specifically the content under Utilizing CodeIgniter Resources within Your Library
请参阅创建库CodeIgniter 文档。特别是在您的库中使用 CodeIgniter 资源下的内容
This assumes you autoload the session library in config/autoload.php, if not, you'll also need to add $CI->load->library("session");after $CIinstantiation.
这假设您在 中自动加载会话库config/autoload.php,如果没有,您还需要$CI->load->library("session");在$CI实例化后添加。
IMPORTANT:=&is not a typo. It's passed by reference to save memory.
重要提示:=&不是错别字。它通过引用传递以节省内存。
回答by ashdaily
You should simply go to application/autoload.php and add your autoload package which should look like somewhat like this : $autoload['packages'] = array('database','form_validation','session','email');
你应该简单地转到 application/autoload.php 并添加你的自动加载包,它应该看起来像这样: $autoload['packages'] = array('database','form_validation','session','email');
you can see there is session package that i added in my packages. Now coming to your constructor you should load this package by adding this : $this->load->library("session");
你可以看到我在我的包中添加了会话包。现在来到你的构造函数,你应该通过添加这个来加载这个包:$this->load->library("session");
回答by Italo Hernández
Session and any other lib / helper , etc extendsfrom CI_Controller / CI_Model / etc...
If you are trying to use $this->whateveron a library that doesn't extends from any of this CI modules, you'll get the error.
As Jordan says, you can use get_instance.
会话和任何其他的lib /帮手等延伸从是CI_Controller / CI_Model的/ etc ...如果你要使用$this->whatever在不从任何该CI模块的扩展库,你会得到错误。正如 Jordan 所说,您可以使用 get_instance。

