2016-08-20 5 views
0

私はcodeigniterプロジェクトに取り組んでいます。私は彼がクラスをロードするたびにユーザーのログインを確認するようにフックを作成したいと思います。 私のフックlocalhostがあなたをリダイレクトしました。 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')); 
     } 
    } 
} 

エンドユーザーのセッションログイン確認を確認します。ユーザーがログインしていない場合は、ログインページにリダイレクトします。ここ は、それがログインコントローラにリダイレクトする場合は、ローカルシステム上にあるもの 「localhostにリダイレクトされます何度も」 を作業するので、それは ようなエラーが表示さ

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

をリダイレクトする私のコントローラでありますこの実際の問題。どんな助けもありがとう。

+0

uは、このサンプル ような何かを試すことができます**リダイレクト( '/ index.phpを/ it_inventory/GET_USERS'、 'リフレッシュ'); **上記I –

+0

を持っています第2引数として 'refresh'を追加しました。この問題が発生しているすべてのリダイレクトにこれを追加してください。 –

+0

私もそれを試みました、それはページを再帰的にリフレッシュします。それはページをロードしていません..任意の他のソリューション..してください – Rakesh

答えて

0

あなたがログインコントローラにいないかどうか、つまり条件が満たされない場合にリダイレクトするログイン以外のすべてのコントローラでフック機能を使用するかどうかを確認する必要があります。あなたは条件に、このようにそのコントローラを除外することができます

/** 
* 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')); 
     } 
    } 
} 
関連する問題