所有管理员/用户功能检查他/她是否已登录 - 如何避免这种情况?

问题描述:

I am very, very new with MVC (just started yesterday) so this question is probably stupid, but I need to know how to check automatically if user is logged in on my functions that are in admin/user models.

I can put the checking in construct, this would help, but I have several models and maybe there is some even better way. I hope you will understand better what I want after you see my folder structure and code. Oh, and by the way - I use Code Igniter 2.0

Folders:

controllers/
../admin/
../../item.php
../../cat.php

Let's see my item.php file...

<?php

class Item extends CI_Controller
{

    function Index()
    {

        //Checking if admin is logged in on every function is bad

        /* 
         * Is it possible to somehow make all admin functions go through
         * some kind of Admin class that will check automatically?
        */
        $isLogged = $this->session->userdata('is_logged_in');

        if ($isLogged == true)
        {
            $this->load->view('admin/item/main');
        }
        else
        {
            $this->load->view('admin/login');
        }
    }

    function Add()
    {
        $this->load->view('admin/item/add');
    }

    function Edit()
    {
        $this->load->view('admin/item/edit');
    }

    function Delete()
    {
        $this->load->view('admin/item/delete');
    }
}

I hope that this is easy question, thanks in advance :)

我是非常非常新的MVC(刚开始昨天)所以这个问题可能很愚蠢,但我需要 知道如果用户登录我的管理员/用户模型的功能,如何自动检查。 p>

我可以将检查置于构造中,这会有所帮助,但我有几个模型和 也许还有更好的方法。 我希望您在看到我的文件夹结构和代码后能够更好地理解我想要的内容。 哦,顺便说一下 - 我使用Code Igniter 2.0 p>

文件夹: p>

  controllers / 
 ../ admin / 
  ../../item.php
../../cat.php
nn

让我们看看 item.php strong> file ... p>

 &lt;?php 
 
class Item extends CI_Controller 
 {
 
函数Index()
 {  
 
 //检查管理员是否登录了每个功能都不好
 
 / * 
 *是否可以以某种方式使所有管理功能通过
 *某种将自动检查的Admin类?  
 * / 
 $ isLogged = $ this-&gt; session-&gt; userdata('is_logged_in'); 
 
 if if($ isLogged == true)
 {
 $ this-&gt; load-&gt  ;查看('admin / item / main'); 
} 
其他
 {
 $ this-&gt; loading-&gt; view('admin / login'); 
} 
} 
 
 
 \  n函数Add()
 {
 $ this-&gt; load-&gt; view('admin / item / add'); 
} 
 
函数Edit()
 {
 $ this-&gt  ;负载&gt;查看( '管理员/项目/编辑'); 
  } 
 
函数Delete()
 {
 $ this-&gt; load-&gt; view('admin / item / delete'); 
} 
} 
  code>  pre>  
 
 

我希望这是一个简单的问题,提前谢谢:) p> div>

I would implement the login-function in CI_Controller. Then I would set an protected variable in Item protected $loginRequired = true;.

In function __construct() or Item() I would call parent::isLoginRequired($this->loginRequired) which checks if a login is required.

I would also redirect to a specific login page with a parameter which redirects the user back to the page he needs to be logged in.

Make a new class, for example, My_Controller extends Ci_Controller and write some auth checker code in it... in controller file just extend My_Controller

what i usually do is -like Teeed recommends- Create my own controller Class which is between the CI_Controller and each controller you might create.

In that class (MY_Controller) you can instantiate a model which handles all user related data and logics (loading session data, executing specific checks, etc..) and finally set as class variables those results, so you will end up having:

$this->isLogged ; $this->isPaying ; $this->isPlatinumMember ; etc..

in any of your classes extending from MY_Controller

that makes very easy to check any condition within any of your Controllers.