codeIgniter只允许访问特定的控制器登录时 [英] CodeIgniter only allow access to certain controllers when logged in

查看:146
本文介绍了codeIgniter只允许访问特定的控制器登录时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有只应由谁已经登录的用户访问一些codeIgniter控制器(即,其中$这个 - >会话级>用户数据(用户名)不为空)。如果非认证的人试图访问所述控制器,他们应获得:

I have some CodeIgniter controllers which should only be accessed by users who have logged in (i.e. where $this->session->userdata('username') is not null). If a non-authenticated person attempts to access said controllers they should receive:

header('location: /auth/login');

有一定是更好的方法来做到这一点,而不是把

There has got to be a better way to do this than to put a

if (!$this->session->userdata('username'))
    header('location: /auth/login');
else
{
    [rest of function]
}

在控制器各功能的前...

in front of every function in the controller...

我知道DX_Auth也有类似的功能,但我不使用身份验证的插件,我不开这样做。

I know DX_Auth has a similar functionality, but I am not using an authentication plugin and I am not open to doing so.

谢谢!结果
麻辣

Thanks!
Mala

推荐答案

执行用户登录检查创建类的时候,所以也无所谓什么功能的用户访问,将检查会话变量并重定向到在失败的登录页面:

Do the user login check when the class is created, so it doesn't matter what function the user is accessing it will check for the session variable and redirect to the login page on failure:

function className()
{
    parent::Controller();
    if(!$this->session->userdata('username')) header('location: /auth/login');
}

这是调用的 __构造的方式或在创建它在codeigniter相当于控制器/型号 ,或者至少这是我的理解!

That's the way of calling the __constructor or it's equivalent in codeigniter when you create controllers/models, or at least that's what I understood!

这篇关于codeIgniter只允许访问特定的控制器登录时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆