我在这个构造函数中有一个名为 member 的 controller
function __construct()
{
parent::Controller();
$this->is_logged_in();
}
我想在我的另一个 Controller 中检查用户是否已登录我如何在我的另一个名为配置文件和其他 Controller 中使用此功能
这是我使用 CodeIgniter 的第一个项目
最佳答案
您的身份验证检查应该在库中:
这是基本的 codigniter 身份验证脚本的摘录:
class Site_sentry
{
function Site_sentry()
{
$this->obj =& get_instance();
}
function is_logged_in()
{
if ($this->obj->session)
{
if ($this->obj->session->userdata('session_logged_in'))
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
function login_routine()
{
//do login here (enter into session)
}
}
此库存储在 application/libraries 中,文件名以其类命名并带有 .php 后缀。
然后你可以将它添加到你的自动加载配置文件 application/conig/config.php:
$autoload['libraries'] = array('database', 'site_sentry', 'session');
或者在每个 Controller 中手动加载它:
$this->load->library('Site_sentry);
然后你可以在 Controller 中检查你的 session ,像这样:
class Class extends Controller{
function Class()
{
parent::Controller();
if( $this->site_sentry->is_logged_in() == FALSE){
redirect('managerlogin/');
}
}
}
另请查看此文档页面 http://codeigniter.com/user_guide/libraries/sessions.html ;特别感兴趣的是将 session 存储到数据库部分。
关于php - 我如何在我的其他 CodeIgniter Controller 中使用 __construct 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3234296/