2011-05-07 43 views
20

我有一個控制器,映射到我的網站的一部分,其中的所有頁面(方法)應該只在用戶登錄時出現,否則應該重定向回到登錄屏幕。CodeIgniter:檢查用戶是否登錄了多個頁面

得到它的工作我只是這樣做:

function index() { 

    if ($this->session->userdata('logged_in')) { 
     $this->load->view('main'); 

    } else { 
     redirect('/login'); 
    } 
} 

function archive() { 

    if ($this->session->userdata('logged_in')) { 

等等...重複每個方法檢查。對控制器中的多重或全部方法進行一次檢查的最簡單方法是什麼?

+0

您的排除方法,我建議ÿ ou爲用戶進程使用庫。 Aauth對此很有幫助。 https://github.com/emreakay/CodeIgniter-Aauth – 2013-10-05 14:20:40

回答

41

您可以通過在__construct()方法運行它運行在一個控制器的每一個方法的代碼:

function __construct() 
{ 
    parent::__construct(); 
    if (! $this->session->userdata('logged_in')) 
    { 
     // Allow some methods? 
     $allowed = array(
      'some_method_in_this_controller', 
      'other_method_in_this_controller', 
     ); 
     if (! in_array($this->router->fetch_method(), $allowed) 
     { 
      redirect('login'); 
     } 
    } 
} 

你可以,如果你想限制對整個事情去掉「允許」位,但有更好的方法來做到這一點,如創建一個基本的控制器:

// Create file application/core/MY_Controller.php 
class Auth_Controller extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     if (! $this->session->userdata('logged_in')) 
     { 
      redirect('login'); 
     } 
    } 
} 

然後讓你的限制控制器擴展Auth_Controller而不是CI_Controller。現在您的代碼將在每次加載控制器時運行。

上擴大核心課程更多信息:http://www.codeigniter.com/user_guide/general/core_classes.html#extending-core-class

的興趣也:http://php.net/manual/en/language.oop5.decon.php

+0

完美,謝謝你。我想我會使用一個基礎控制器。 – gio 2011-05-08 05:50:40

+0

爲了使'redirect()'工作,需要加載URL助手:'$ this-> load-> helper('url');' – user2019515 2013-04-13 01:09:45

+0

您將新控制器的文件命名爲MY_Controller.php,但您將類命名爲Auth_Controller 。這不應該工作,對吧?我在問,因爲我遇到了非常奇怪的事情:只要我命名我的新控制器(它擴展了基本控制器)MY_Auth_Controller,並試圖將它用作我的控制器的基類,CodeIgniter一直在崩潰。當我命名如果'MY_Controller'它一切運作良好。這是爲什麼? – 2014-01-02 20:29:47

1

我用這個函數:

然後,只需調用$這個 - > isAuthorized從你的控制器__construct。

它允許我控制訪問哪些控制器以及訪問哪些方法。

protected function isAuthorized() 
{ 

    switch (strtolower($this->router->class)) 
    { 
     case 'pages': 
      $disallowLoggedOut = array('dashboard'); 
      $disallowLoggedIn = array('index'); 
     break; 

     case 'users': 
      $disallowLoggedOut = array('logout'); 
      $disallowLoggedIn = array('register', 'login'); 
     break; 
    } 

    if ($this->session->userdata('loggedIn')) 
    {  
     if (in_array($this->router->method, $disallowLoggedIn)) 
     { 
      redirect('pages/dashboard'); 
     } 
    } 
    else 
    {  
     if (in_array($this->router->method, $disallowLoggedOut)) 
     { 
      redirect('pages/index'); 
     } 
    } 
} 
4

對於笨3我修改韋斯利默奇的回答這個

//創建文件的應用程序/核心/ MY_Controller。PHP

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
class MY_Controller extends CI_Controller { 

function __construct() 
{ 
    parent::__construct(); 
    $CI = & get_instance(); 
    $CI->load->library('session'); 
    $CI->load->helper('url'); 
    if (!$this->session->userdata('logged_in')) 
    { 
     redirect('login'); 
    } 
} 

}

然後在任何控制器來檢查我用

類新聞延伸MY_Controller {// 代碼在這裏 }

如果您授權爲網站用戶和管理員使用模塊和不同的會話在用戶,您可以使用此代碼來完美地重定向到不同的登錄pages-

function __construct() { 
    parent::__construct(); 
    $CI = & get_instance(); 
    $CI->load->library('session'); 
    $CI->load->helper('url'); 
    // echo "<pre>";print_r($this->router);echo "</pre>"; 

    /** 
    * if webmaster then check admin session else check user session 
    * But there may be some classes's method that doesn't requires login hence it is also need to check if 
    * current request is for those methods before checking session 
    */ 
    //to use $this->config->item('webmaster_name') this you have to define 
    // $config['webmaster_name'] = "webmaster"; in config.php file 

    if ($this->router->module == $this->config->item('webmaster_name')) { 
     if (!$this->session->userdata('admin')['id']) { 
      redirect($this->config->item('webmaster_name').'/login'); 
     } 
    } else { 
     if (!$this->session->userdata('user')['id']) { 
      redirect('login'); 
     } 
    } 
} 

如果你也希望用戶能夠訪問來自任何特定的控制器的一些方法,而不登錄您可以使用此代碼 -

function __construct() { 
    parent::__construct(); 
    $CI = & get_instance(); 
    $CI->load->library('session'); 
    $CI->load->helper('url'); 

    //echo "<pre>"; print_r($this->router);echo "</pre>"; //_pr($this->config->item('excluded_auth')); 
    /** 
    * if webmaster then check admin session else check user session 
    * But there may be some classes's method that doesn't requires login hence it is also need to check if 
    * current request is for those methods before checking session 
    */ 
    if ($this->router->module == $this->config->item('webmaster_name')) { 
     if (!$this->session->userdata('admin')['id']) { 
      redirect($this->config->item('webmaster_name') . '/login'); 
     } 
    } else { 
     if (array_key_exists($this->router->class, $this->config->item('exclude_auth')) && in_array($this->router->method, $this->config->item('exclude_auth')[$this->router->class])) { 
      //echo "escape this method. don not validate for a session"; 
     } else { 
      if (!$this->session->userdata('user')['id']) { 
       redirect('login'); 
      } 
     } 
    } 
} 

注:您可以定義自定義配置文件定義像原樣

//save file in application/config/without_auth_methods.php 

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
    $config['exclude_auth']['news']  = array('index', 'view'); 
    $config['exclude_auth']['users']  = array('index'); 
+0

謝謝!很好解釋! – Minoru 2017-02-23 17:40:02