使用 CodeIgniter 的 Pre Controller Hook 示例

application/hooks 文件夹中,创建名为 Blocker.php 的文件并粘贴以下代码。

<?php
class Blocker {

    function Blocker(){
    }
    
    /**
     * This function used to block the every request except allowed ip address
     */
    function requestBlocker(){
        
        if($_SERVER["REMOTE_ADDR"] != "49.248.51.230"){
            echo "not allowed";
            die;
        }
    }
}
?>

application/config/hooks.php 中,声明以下钩子。

$hook['pre_controller'] = array(
        'class'    => 'Blocker',
        'function' => 'requestBlocker',
        'filename' => 'Blocker.php',
        'filepath' => 'hooks',
        'params'   => ""
);

application/config/config.php 中,将以下值设置为 true