Codeigniter - Hook to log GET / POST REQUESTS [英] Codeigniter - Hook to log GET / POST REQUESTS
问题描述
客户端要求所有GET / POST请求都记录并存储90天以进行应用。我写了一个HOOK似乎记录一些GETS / POSTS,但有比预期少的数据。例如,在提交表单数据时,这些条目似乎不会放在日志中。有没有人写过类似的工作?
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 there is less data than I would expect. For example, when submitting form data, the entries don't seem to be put in the log. Has anyone written something similar which works?
这是我的版本到目前为止:
Here is my version thus far:
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')
));
}
}
称为Logging_m,如下所示:
This data is stored in a model called 'Logging_m' which looks like this:
<?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 */
推荐答案
正如Patrick Savalle提到的,你应该使用钩子。使用 post_controller_constructor
钩子,以便您可以使用所有其他CI材料。
As mentionned by Patrick Savalle you should use hooks. Use the post_controller_constructor
hook so you can use all other CI stuff.
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()
);
3)创建文件 ./ application / hooks / http_request_logger.php
并添加以下代码作为示例。
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).
这篇关于Codeigniter - Hook to log GET / POST REQUESTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!