codeigniter Pre Controller Hook example using CodeIgniter


Example

In application/hooks folder, create a file with name Blocker.php and paste the below code.

<?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;
        }
    }
}
?>

In application/config/hooks.php, declare the following hook.

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

In application/config/config.php, set following value as true