Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - Hook to log GET / POST REQUESTS

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

}

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 */
like image 278
James Stoddern Avatar asked Jan 07 '13 11:01

James Stoddern


1 Answers

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

like image 122
Chris Aelbrecht Avatar answered Sep 28 '22 10:09

Chris Aelbrecht