CodeIgniter:尝试调用构造函数方法以检查用户是否已登录(导致无限重定向循环) [英] CodeIgniter: trying to call constructor method to check if user is logged in (causes endless redirection loop)

查看:97
本文介绍了CodeIgniter:尝试调用构造函数方法以检查用户是否已登录(导致无限重定向循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了CodeIgniter 2.1.2的问题,并停留了几个小时尝试解决它:-/

I've got a Problem with CodeIgniter 2.1.2 and stuck for hours try to solve it :-/

我知道有很多线程(!),但是我找不到解决问题的方法.

I know there are plenty(!) of threads about that, but i couldn't find a solution for my problem.

我想在我的构造函数中加载一个方法来检查用户是否已登录,所以我尝试了此操作:

I want to load a method in my constructor to check if a user is logged in, so i tried this:

class my_class extends CI_Controller
{
public function __construct();
parent::__construct();

$this->check(); //doesn't work, endless redirection loop
}

"check()"为:

"check()" is:

public function check()
{
    if (! $this->session->userdata('logged_in'))  
    {     
        redirect('/login'); // tried with & without '/'
    }
}

方法"login()"如下所示:

method "login()" looks like this:

public function login()
{
  ...
  //do form validation stuff and on success:
  if ($this->form_validation->run() == TRUE)
    {
     $this->session->set_userdata('logged_in', TRUE);
     redirect('/entry'); 
    }
   //load login_view
}

方法"entry()":

method "entry()":

public function entry()
{
  //$this->check();//Old (redundant) Version

  //Authorized and Happy...
}

//其他一些方法,也首先调用check()方法

// some other methods, also call the check() method first

所以我的问题是,为什么方法中的调用有效(但是有很多多余的调用) 和构造函数中的调用给我一个无尽的循环?我会错过什么吗? (我也没有更改htaccess文件中的内容.)

So my question is, why the calls in the methods works (but with much redundant calls) and the call in the constructor give me an endless loop? Do I miss something?? (I also didn't change stuff in the htaccess files..)

事先感谢:-)

推荐答案

要详细说明评论者所说的话:

To elaborate on what commenters have said:

暂时完全忽略MY_Controller解决方案,问题是在每个控制器(包括登录控制器)上都调用了__construct()方法-随后是$this->check()-.

Ignoring the MY_Controller solution entirely for a moment, the issue is that your __construct() method -- and subsequently, $this->check() -- is being called on every controller, including your login controller.

  1. 加载主页.构造函数加载check().
  2. 未登录.重定向到/login.
  3. 登录构造函数加载check().
  4. 未登录.重定向到/login.
  1. Load homepage. Constructor loads check().
  2. Not logged in. Redirect to /login.
  3. Login constructor loads check().
  4. Not logged in. Redirect to /login.

...等等,依此类推.

... and so on and so forth.

解决方案是检查正在访问的URL ,如果它属于您的登录控制器/方法,则不要执行重定向.

The solution is to check what URL is being accessed, and if it belongs to your login controller/method, then don't perform the redirect.

public function check()
{
    if ($this->uri->uri_string() !== 'login' && ! $this->session->userdata('logged_in'))
    {     
        redirect('login');
    }
}

相反,您的登录方法应检查用户是否确实已经登录,如果已登录,则将其重定向到您的主页或其他内容.但是我离题了.

Inversely, your login method should check if the user is indeed already logged in, and if so, redirect him to your homepage or something. But I digress.

MY_Controller解决方案涉及创建一个基本控制器,其构造函数执行已登录的检查.然后,要执行该检查的所有控制器都应扩展MY_Controller而不是CI_Controller.

The MY_Controller solution involves creating a base controller whose constructor performs the logged in check. Then, any controllers where you want to perform that check should extend MY_Controller instead of CI_Controller.

请注意,您的登录控制器将扩展MY_Controller,因为您不想执行检查,否则无限循环将再次出现.

Note that your login controller will not extend MY_Controller, because you don't want to perform the check, or your infinite loop will show up again.

这篇关于CodeIgniter:尝试调用构造函数方法以检查用户是否已登录(导致无限重定向循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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