Codeigniter - Hook to log GET / POST REQUESTS
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).
![James Stoddern](https://i.stack.imgur.com/lOKKp.jpg?s=256&g=1)
Comments
-
James Stoddern about 2 years
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 */
-
Vipin Kr. Singh almost 9 yearsis it OK to declare $CI as public in a hook file?
-
Chris Aelbrecht over 8 years@VipinKr.Singh Looking at it again I don't see any reason to declare it outside the function at all. I've modified the example.
-
Web Artisan about 8 yearsNothing is writing in info page neither page is created from your code. :(
-
Chris Aelbrecht about 8 years@BikashP I can assure you it worked for me 2 years ago (I only put tested code on SO). Also the fact that it was already marked 7 times as useful answer probably means it works. You're for sure missing something else in your code on your side.