dongya4089 2011-03-06 14:59
浏览 29
已采纳

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

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 :)

  • 写回答

3条回答 默认 最新

  • dongyuans61046 2011-03-06 15:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP