dongyi7966 2013-03-14 15:55
浏览 49
已采纳

CodeIgniter:尝试调用构造函数方法来检查用户是否已登录(导致无限重定向循环)

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()" is:

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

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
}

method "entry()":

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

  //Authorized and Happy...
}

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

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

Thx in advance :-)

  • 写回答

3条回答 默认 最新

  • doutao1939 2013-03-15 01:56
    关注

    To elaborate on what commenters have said:

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

    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.


    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.

    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.

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算