【问题标题】:Codeigniter - Hook to log GET / POST REQUESTSCodeigniter - 挂钩记录 GET / POST 请求
【发布时间】:2013-01-07 11:47:31
【问题描述】:

客户端要求记录所有 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')
        ));
    }

}

这些数据存储在一个名为“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 */

【问题讨论】:

  • 您能否发布您缺少操作的表单。
  • 有趣的问题+1。您能否对表单输入进行 var_dump 并将其发布在日志旁边以获取相同的输入,以便我们可以发现差异。
  • 您的所有数组值是否都按照您的预期设置,还是只是 GET/POST 信息?另外,我很好奇,当 $this->input->post() 已经返回数据时,为什么要使用 print_r 返回 $_POST 数组。你似乎没有清理数据,只是修剪它。这似乎是多余的。
  • 你应该为此使用钩子。可能是 'pre_controller': codeigniter.com/user_guide/general/hooks.html 最好使用 $_GET 和 $_POST。
  • 嗯,我只是看到我下面的答案是你已经拥有的。我没有很好地阅读你的帖子。通常使用钩子它应该记录所有通过 CI_Controller 的请求。

标签: codeigniter post logging get hook


【解决方案1】:

正如 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()
);

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

}

我已经对其进行了测试,它适用于我(确保您已在配置文件中激活日志记录)。

【讨论】:

  • 可以在挂钩文件中将 $CI 声明为公开吗?
  • @VipinKr.Singh 再看一遍,我看不出有任何理由在函数之外声明它。我已经修改了示例。
  • 信息页面中没有写入任何内容,也没有从您的代码创建页面。 :(
  • @BikashP 我可以向你保证,它在 2 年前对我有用(我只在 SO 上放置了经过测试的代码)。此外,它已经被标记为 7 次有用的答案这一事实可能意味着它有效。您肯定会在您的代码中遗漏其他内容。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2010-10-31
  • 2017-06-29
  • 2021-10-06
  • 1970-01-01
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多