Codeigniter - Hook to log GET / POST REQUESTS

 ̄綄美尐妖づ 提交于 2019-12-02 19:45:43

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!