Codeigniter - 挂钩记录GET/POST请求

Jam*_*ern 7 hook post logging codeigniter get

客户端要求记录所有GET/POST请求并将其存储90天以用于其应用程序.我写了一个HOOK,似乎记录了一些GETS/POSTS,但数据少于我的预期.例如,在提交表单数据时,条目似乎不会放入日志中.有人写过类似的东西吗?

这是我到目前为止的版本:

class Logging {

    function __construct() {
        $this->CI =& get_instance();
    }

    function index() {
        $this->CI->load->model('Logging_m');
        $this->CI->load->model('Portal_m');

        //get POST and GET values for LOGGING        
        $post = trim(print_r($this->CI->input->post(), TRUE));
        $get = trim(print_r($this->CI->input->get(), TRUE));

        $this->CI->Logging_m->logPageView(array(
                'portal_id' => $this->CI->Portal_m->getPortalId(),
                'user_id' => (!$this->CI->User_m->getUserId() ? NULL : $this->CI->User_m->getUserId()),
                'domain' => $_SERVER["SERVER_NAME"],
                'page' => $_SERVER["REQUEST_URI"],
                'post' => $post,
                'get' => $get,
                'ip' => $this->CI->input->ip_address(),
                'datetime' => date('Y-m-d H:i:s')
        ));
    }

}
Run Code Online (Sandbox Code Playgroud)

此数据存储在名为"Logging_m"的模型中,如下所示:

<?php 
class Logging_m extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  function logPageView($data) {
    $this->db->insert('port_logging', $data);
  }

}

/* End of file logging_m.php */
/* Location: ./application/models/logging_m.php */
Run Code Online (Sandbox Code Playgroud)

Chr*_*cht 15

正如Patrick Savalle所提到的,你应该使用钩子.使用post_controller_constructor钩子,以便您可以使用所有其他CI的东西.

1)在./application/config/config.php集合中$config['enable_hooks'] = TRUE

2)./application/config/hooks.php添加以下钩子

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

3)创建文件./application/hooks/http_request_logger.php并添加以下代码作为示例.

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

}
Run Code Online (Sandbox Code Playgroud)

我已经测试了它,它适用于我(确保在配置文件中激活了日志记录).