Codeigniter - Hook to log GET / POST REQUESTS

后端 未结 1 1342
长发绾君心
长发绾君心 2021-02-03 13:43

A client requires that all GET/POST requests are logged and stored for 90 days for their applicaiton. I have written a HOOK which seems to record some of the GETS / POSTS but th

1条回答
  •  故里飘歌
    2021-02-03 14:17

    As mentionned by Patrick Savalle you should use hooks. Use the post_controller_constructor hook so you can use all other CI stuff.

    1) In ./application/config/config.php set $config['enable_hooks'] = TRUE

    2) In ./application/config/hooks.php add the following hook

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

    3) Create the file ./application/hooks/http_request_logger.php and add the following code as example.

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    
    class Http_request_logger {
    
        public function log_all() {
            $CI = & get_instance();
            log_message('info', 'GET --> ' . var_export($CI->input->get(null), true));
            log_message('info', 'POST --> ' . var_export($CI->input->post(null), true));                
            log_message('info', '$_SERVER -->' . var_export($_SERVER, true));
        }
    
    }
    

    I've tested it and it works for me (make sure you have logging activated in your config file).

    0 讨论(0)
提交回复
热议问题