Codeigniter - 挂钩以记录 GET/POST 请求 [英] 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?
这是我目前的版本:
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
中添加如下钩子
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) 创建文件 ./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 - 挂钩以记录 GET/POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!