douqin7086 2016-08-20 05:13
浏览 53

localhost重定向了你太多次了。 在codeigniter钩子

I;m working on a codeigniter project. I want to create a hook so that, it verify the user login every time when he load the class. my Hook

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

and also enabled in the config file.

/**
* 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'));
        }
    }
}

It checks for the session login verify for the end user. If user not logged in then redirect to the login page. Here is my Controller where it redirects

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');
    }
}

When it redirect to the login controller, it show errors like since it working on the local system "localhost redirected you too many times" what is the actual problem with this. Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • doubinduo3364 2016-08-20 13:34
    关注

    You want to check if you are not on login controller or in other words you want to have hook functionality on every controller except login where you redirect if condition not met. You can exclude that controller in condition this way:

    /**
    * 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'));
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应