笨用户登录检查

问题描述:

我用代码点火器是新手,目前我正在建立自己的用户系统。我很遗憾地登录过程,并且已经实现了一个用户当前是否登录的检查。笨用户登录检查

在我的头,然后我想显示一个链接到“注销”,如果他们已经登录,或“登录”如果他们没有在当前登录。

我有一个工作功能在我的索引控制器如下,在$ loginstatus变量发送到我的网页标题观点:$ loginstatus不

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; 
} 

因为它是目前唯一索引控制器为其他页面的标题视图生成,这是我的问题。

我会在哪里把这个功能,所以它总是我的头之前加载?我试图创建一个与'普通'类的库,然后自动加载,但我最终有很多问题。

在此先感谢。

如果您正在使用CI版本波纹管2.0,然后创建新的应用类/库/ MY_Controller.php,否则在应用/核心/ MY_Controller.php和所有应用程序控制器应该从其扩展。在这个类的__construct方法中,您将检查登录状态并将其发送到视图。

 
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 
    } 
} 

另外,还要确保您的应用程序控制器从这个类

 
//application/controllers/index.php 

class Index extends MY_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 
} 

延长在你的意见,你可以这样做:? < A HREF =” < PHP的echo $ loginstatus [ '链接']; ?>“> < PHP的echo $ loginstatus [ '状态']; ? > </a >

这可能会导致CI_Loader vars()方法正在执行传递给它的参数extract

+0

我想我明白你的意思,但我对'MY_Controller'有困难我以前试过使用这个,但是当我这样做时我得到以下错误: ** bold **致命错误:类'MY_Controller'not在/home/chimeri1/public_html/application/controllers/welcome.php发现第3行** **大胆 – James 2011-04-10 19:56:43

+0

我使用CodeIgnitier 2.0顺便说一下,谢谢你的帮助。 – James 2011-04-10 19:58:14

+0

更新:我有MY_Controller工作(我把它放在系统/核心,而不是应用程序/核心!),但我不认为你的第二行关于发送变量的意见是为我工作,你能解释这是怎么回事作品?谢谢。 – James 2011-04-10 20:18:52