【问题标题】:localhost redirected you too many times. in codeigniter hookslocalhost 将您重定向了太多次。在 codeigniter 钩子中
【发布时间】:2016-08-20 05:13:58
【问题描述】:

我正在开发一个 codeigniter 项目。我想创建一个钩子,以便在每次加载类时验证用户登录。 我的钩子

$hook['post_controller_constructor'] = array(
        'class'    => 'Auth_login',
        'function' => 'is_logged_in',
        'filename' => 'Auth_login.php',
        'filepath' => 'hooks',
);

并且也在配置文件中启用。

/**
* Auth_login
*/
class Auth_login
{
    /**
     * loading the CI instance
     * @var [object]
     */
    private $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('session');
        $this->CI->load->helper('url');
    }

    /**
     * Check the enduser for login verify, every time when the enduser
     * loads the controller.
     */
    public function is_logged_in() 
    {
        /**
        *  if end user is not logged in, redirect to the login page
        */  
        if(!$this->CI->session->userdata('is_logged_in')) 
        {
            $this->CI->session->set_tempdata('faliure','Please sign in to continue');
            redirect(site_url('login/index'));
        }
    }
}

它检查最终用户的会话登录验证。如果用户未登录,则重定向到登录页面。 这是我的控制器重定向的地方

class Login extends CI_Controller {
    // class constructor
    //    public function __construct() {
    //     parent::__construct();
    //         // load default 
    //     $this->load->model('login_model');
    // }

    /**
     * [display the login page, and verify the login page using "form_validation" for server side validation]
     * @return [redirect] [description]
     */
    public function index() 
    {
        $this->load->model('login_model');
        // on form sumbit
        $on_sumbit = $this->input->post('verify');
        if(isset($on_sumbit)) 
        {
            // server side validation for login
           if ($this->form_validation->run('login/login_verify') == FALSE)
            {
                $this->load->view('login');
            }
            else 
            {
                // get the form post value
                $user_name = $this->input->post('user_name');
                $password = $this->input->post('password');
                // verify login
                $verified = $this->login_model->login_verify($user_name,$password);
                if($verified)  // success
                {
                    redirect('dashboard');
                }
                else // failure
                {
                    $this->session->set_flashdata('login_failure','Please check your email and password and try again');
                    redirect('login/index');
                }
            }
        }
        // login page
        $this->load->view('login');
    }

    // delete active session
    public function logout() 
    {
        $this->session->sess_destroy();
        redirect('login');
    }
}

当它重定向到登录控制器时,它会显示如下错误 因为它在本地系统上工作 "localhost 重定向您的次数过多" 这有什么实际问题。任何帮助,将不胜感激。

【问题讨论】:

  • 你可以试试这个示例 redirect('/index.php/it_inventory/get_users', 'refresh');
  • 在上面我添加了“刷新”作为第二个参数..在您遇到此问题的所有重定向中添加这个。
  • 我也试过了,它会递归刷新页面。它不加载页面..任何其他解决方案..请

标签: php .htaccess redirect hook codeigniter-3


【解决方案1】:

你想检查你是否不在登录控制器上,或者换句话说,你想在每个控制器上都有挂钩功能,除了登录,如果条件不满足,你会重定向。您可以通过以下方式排除该控制器:

/**
* Auth_login
*/
class Auth_login
{
    /**
     * loading the CI instance
     * @var [object]
     */
    private $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('session');//working with sessions - you want sessions 
        //to be loaded ASAP so better way would be having it in 
        //APPPATH.'config/autoload.php' instead in all classes

        /** 
         * @tutorial how to exclude hook interaction with particular class/controller
         */
        if ( strtolower( $this->CI->router->class ) == 'login' )
        {
            return;
        }

        $this->CI->load->helper('url');
    }

    /**
     * Check the enduser for login verify, every time when the enduser
     * loads the controller.
     */
    public function is_logged_in() 
    {
        /**
        *  if end user is not logged in, redirect to the login page
        */  
        if(!$this->CI->session->userdata('is_logged_in')) 
        {
            $this->CI->session->set_tempdata('faliure','Please sign in to continue');
            redirect(site_url('login/index'));
        }
    }
}

【讨论】:

    【解决方案2】:

    从 \application\cache\session 中删除所有会话

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 2020-01-03
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多