Codeigniter用户登录检查 [英] Codeigniter Users Login Check
问题描述
我是Code Igniter的新手,我正在建立自己的用户系统。我真的在登录过程,我已经实施了一个检查用户是否当前登录。
I'm a novice with Code Igniter and I'm currently building my own user system. I'm curretly on the login process and I have implemented a check for whether a user is currently logged in or not.
在我的标题中,我想显示一个链接,如果他们已经登录,注销,或登录,如果他们没有当前登录。
In my header I then want to display a link to 'Log Out' if they are already logged in, or 'Log In' if they are not logged in currently.
我在索引控制器中有一个工作函数,如下所示,$ loginstatus变量发送到我的页眉视图:
I have a working function in my index controller as follows, the $loginstatus variable is sent to the my page header view:
function check_session()
{
//Check session status
$session = $this->session->userdata('login_state');
$default = "Log In";
if ($session == 1)
{
$url = site_url('index.php/users/logout');
$status = "Log Out";
}
else
{
$url = site_url('index.php/users/login');
$status = $default;
}
$loginstatus = array(
"url" => $url,
"status" => $status
);
return $loginstatus;
}
因为目前只有在索引控制器中,$ loginstatus不会生成其他页面的头部视图,这是我的问题。
Because it is currently only in the index controller the $loginstatus is not generated for the header view for other pages and this is my problem.
我在哪里放置这个函数,以便它总是加载在我的标题之前?我试着创建一个具有'Common'类的库,然后自动加载,但我结束了很多问题。
Where would I put this function so that it always loads before my header? I tried creating a libary with a 'Common' class and then autoloading that but I ended up with lots of problems.
提前感谢。
推荐答案
如果您使用CI version bellow 2.0,则在application / libraries / MY_Controller.php中创建新类,否则在应用程序/ core / MY_Controller.php和所有的应用程序控制器应该从它延伸。在此类中的__construct方法中,您将检查登录状态并将其发送到视图。
If you are using CI version bellow 2.0 then create new class in application/libraries/MY_Controller.php, otherwise in application/core/MY_Controller.php and all of your application controllers should extend from it. In this class in the __construct method you will check for the login status and send it to the views.
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
//Get the status which is array
$login_status = $this->check_session();
//Send it to the views, it will be available everywhere
//The "load" refers to the CI_Loader library and vars is the method from that library.
//This means that $login_status which you previously set will be available in your views as $loginstatus since the array key below is called loginstatus.
$this->load->vars(array('loginstatus' => $login_status));
}
protected function check_session()
{
//Here goes your function
}
}
同时确保您的应用程序控制器从此类扩展
Also make sure your application controllers extend from this class
//application/controllers/index.php
class Index extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
在你的视图中你可以这样做:
< a href = <?php echo $ loginstatus ['url'];?>><?php echo $ loginstatus ['status']; ?< / a>
In your views you can do this: <a href="<?php echo $loginstatus['url']; ?>"><?php echo $loginstatus['status']; ?></a>
这可能是因为CI_Loader vars()方法正在执行提取到传递给它的参数。
This is possible cause the CI_Loader vars() method is doing extract to the parameters passed to it.
这篇关于Codeigniter用户登录检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!