2017-03-23 50 views
1

嗨,我有一些問題,使我的輸入頁面禁止在CI下。根據所連接的用戶的等級,他將能夠看到或不看到輸入頁面。 這是我在應用程序/鉤/ forbidden.php鉤:如何使頁面訪問CodeIgniter下的鉤子禁止

<?php 
class forbid 
{ 
    private $CI; 
     function forbidden() 
     { 
       if ($this->session->userdata('logged_in')) { 
      foreach ($this->session->userdata('ranked_in') as $rank) 
      { 
       switch ($rank) 
       { 
        case $rank== 1:  //Admin 
         redirect('home', 'refresh'); 
         break; 


        case $rank== 4: //student 
        { 
         if(($_SERVER['REQUEST_URI']) == '/project/input') 
         { 
          redirect ('student','refresh'); 
         } 
        } 
         break; 
       } 
      } 

     }else{ 
     $this->load->helper(array('form')); 
     $this->load->view('login'); 
     } 

     } 

} 
?> 

我在這裏的願望就是當秩的用戶是4重定向到學生的網頁,但是當我測試它沒有任何反應。我犯過錯誤還是錯過了某些東西?謝謝你的幫助 !編輯: 掛鉤在配置文件中啓用。 在配置/鉤子鉤點:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

/* 
| ------------------------------------------------------------------------- 
| Hooks 
| ------------------------------------------------------------------------- 
| This file lets you define "hooks" to extend CI without hacking the core 
| files. Please see the user guide for info: 
| 
| https://codeigniter.com/user_guide/general/hooks.html 
| 
*/ 

$hook['pre-controller'] = array(
     'class' => 'forbid', 
     'function' => 'forbidden', 
     'filename' => 'forbidden', 
     'filepath' => 'hooks', 
     'params' => array() 

    ); 

?> 
+0

什麼是鉤點?掛鉤已啓用? – qwertzman

+0

我認爲問題出在**開關的基礎上**因爲情況不是'case $ rank == 1:'正確的方法是'case 1:' – elddenmedio

+0

@elddenmedio嗯不一定通常都是這兩種語法的作品對我來說...... –

回答

0

很難看到沒有你的代碼的全貌。這是我如何在我最近的一個項目中做到的。

此代碼進入/application/config/hooks.php - 也許您可以複製並調整它以適應您的需求。

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

/* 
| ------------------------------------------------------------------------- 
| Hooks 
| ------------------------------------------------------------------------- 
| This file lets you define "hooks" to extend CI without hacking the core 
| files. Please see the user guide for info: 
| 
| https://codeigniter.com/user_guide/general/hooks.html 
| 
*/ 

// Force Login 
$hook['post_controller_constructor'] = function() 
{ 

    $CI =& get_instance(); 

    $publicPages = array(
     'debug', 
     'login', 
     'logout', 
     'password-reset', 
     'api', 
     'api/3D-complete', 
     'api/refund', 
     'api/3D-enrolment-check' 
    ); 

    $publicRegexPages = array(
     'password-reset-verification', 
     'psp', 
     'cron' 
    ); 

    if(!$CI->session->userdata('isLoggedIn') && !in_array($CI->uri->uri_string, $publicPages) && !in_array($CI->uri->segment(1), $publicRegexPages)) 
    { 

     $intendedPage = $CI->uri->uri_string; 

     redirect('login?redirect=' . $intendedPage); 

    } 

};