2016-12-15 74 views
0

一個登錄會話鉤我在hook.php有這樣的:試圖使笨

$hook['post_controller_constructor'][] = array(
    'class' => 'Account', 
    'function' => 'check_user_login', 
    'filename' => 'authenticate.php', 
    'filepath' => 'hooks/authenticate', 
    'params' => array() 
    ); 

,這在鉤子文件夾authenticate.php:

<?php 
class Account 
{ 
    function check_user_login() 
    { 
     if($this->session->userdata('is_logged_in')){ 
      redirect('pag/index');  
     }else{ 
      redirect('main/restricted'); 
     } 
    } 
} 
?> 

我想要做的登錄會話可用於所有模型/控制器,但有些不對,我也設置了$ config ['enable_hooks'] = TRUE;感謝

回答

1

試試這個(hooks.php

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

現在勾文件(Authenticate.php)在hooks文件夾

<?php 
class Authenticate{ 
    protected $CI; 

    public function __construct() { 
    $this->CI = & get_instance(); 
    } 
    public function check_user_login(){ 
     if(!$this->CI->session->is_logged_in){ 
      redirect('main/restricted');  
     } 
    } 
} 
?> 

您將需要$this->CI =& get_instance()引用CI超級然後最終數據將是可致電$this->CI..

https://www.codeigniter.com/user_guide/general/hooks.html

如果您正在使用笨版本3,那麼可以使用 $this->CI->session->is_logged_in代替 $this->CI->session->userdata('is_logged_in')

+0

它不工作,可能是別的東西是錯誤的,我會得到從控制器404未找到。 – Bogdan

+0

@AnakinVader現在檢查我編輯的答案並測試 –

+0

是的,我用$ this-> CI-> session-> is_logged_in(我有CI 3) – Bogdan