Building an audit log in CodeIgniter

4,687 views
Skip to first unread message

Peter Karunyu

unread,
Oct 6, 2011, 12:10:11 PM10/6/11
to codeigni...@googlegroups.com
I have built this sort of large-ish app in CI and I want to implement an audit log, i.e. capability to track each an every user action.

Anyone who has done something similar and can share ideas and concepts?

I am thinking that there are two alternatives;
1. Implement it in the controllers
Since users have to interact with a controller before they can do anything

2. Implement it in the models
This might be easier since almost all controller functions of high enough value to be logged almost always end up making a model call.

Ideas?

Peter Karunyu

unread,
Oct 7, 2011, 2:59:42 AM10/7/11
to codeigni...@googlegroups.com
Thinking more about this, I want to log the following information:
1. Username and user id of the currently logged in user
2. The "module", this will map to the various distinct sections of my app
3. The "action" i.e. delete, create, update
4. Details of the user agent, using CI's $this->agent->agent_string() so that I end up with a string like "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2"
5. I serialize the $_SERVER variable and store that also as it might contain interesting tidbits like IP address
6. The query being run
7. A timestamp

Now, if I place the above logic in a custom library (so that I can easily use it in all my controllers), how do I get the query from the model?

Joe Murithi Njeru

unread,
Oct 7, 2011, 3:32:48 AM10/7/11
to codeigni...@googlegroups.com
Hello Peter,

I usually keep a variable that stores all the CUD (minus R) SQL queries before I run them. On success of the above queries I implement my audit log. So

if(#this->ModelW->AddNuclearBomb)RunAudit

Hope it helps.
--
Regards,

Joe Murithi Njeru
LPI Certified Professional (www.lpi.org)
Linux User: #361092
Skype ID: joenjeru
Twitter: http://twitter.com/joenjeru 
Blog: http://www.joenjeru.com

joenjeruqr.png

Peter Karunyu

unread,
Oct 13, 2011, 9:49:32 AM10/13/11
to codeigni...@googlegroups.com, joe....@gmail.com
Yeah, that helps. Due to the unique mess my code is in, I ended up making two log entries for such actions, for example, log the fact that a user has "commenced" editing a record, then log the fact the record has been changed.

That way, even if the change did not succeed, there is still a log entry.

But the downside is that the audit table will grow very big very fast.

Brian Rioba

unread,
Oct 31, 2011, 5:37:36 AM10/31/11
to codeigni...@googlegroups.com, joe....@gmail.com
Hi all,
Has anyone done an implementation of any payment solutions in kenya through codeigniter. I wish to develop one and want to know if someone has already done one.

If not, i shall do one and host it somewhere for future use.Any suggestions?

Peter Karunyu

unread,
Nov 6, 2011, 12:14:01 PM11/6/11
to codeigni...@googlegroups.com
Note to self, came across a tutorial on MySQL triggers which can more or less achieve the same goal:

CREATE TRIGGER `after_insert_cart_items` 
    AFTER INSERT ON `trigger_cart_items` FOR EACH ROW 
    BEGIN 
        INSERT INTO trigger_cart_log (cart_id, item_id) 
        VALUES (NEW.cart_id, NEW.item_id); 
    END

Source: http://net.tutsplus.com/tutorials/databases/introduction-to-mysql-triggers/

Isaak Mogetutu

unread,
Nov 7, 2011, 2:59:39 AM11/7/11
to codeigni...@googlegroups.com
Use of hook, pre_controller, pre_system hooks, the works to pick user activities during system use. Enable hooks in config file
Within the system/application/config/hooks.php file 

$hook['post_controller_constructor'] = array(
    'class'    => 'Statistics',
    'function' => 'log_activity',
    'filename' => 'Statistics.php',
    'filepath' => 'hooks'
);

The SQL schema
CREATE TABLE IF NOT EXISTS `statistics` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `account_id` int(11) NOT NULL,
    `project_id` int(11) NOT NULL,
    `user_id` int(11) NOT NULL,
    `section` varchar(32) NOT NULL,
    `action` varchar(32) NOT NULL,
    `when` int(11) NOT NULL,
    `uri` varchar(255) NOT NULL,
    PRIMARY KEY (`id`)
)


under hooks folder in your application/hooks/statistics.php

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Statistics {
    public function log_activity() {
        // We need an instance of CI as we will be using some CI classes
        $CI =& get_instance();
 
        // Start off with the session stuff we know
        $data = array();
        $data['account_id'] = $CI->session->userdata('account_id');
        $data['project_id'] = $CI->session->userdata('project_id');
        $data['user_id'] = $CI->session->userdata('user_id');
 
        // Next up, we want to know what page we're on, use the router class
        $data['section'] = $CI->router->class;
        $data['action'] = $CI->router->method;
 
        // Lastly, we need to know when this is happening
        $data['when'] = time();
 
        // We don't need it, but we'll log the URI just in case
        $data['uri'] = uri_string();
 
        // And write it to the database
        $CI->db->insert('statistics', $data);
    }
}


Enjoy
Message has been deleted

David Mwangi

unread,
Jan 14, 2015, 8:54:51 AM1/14/15
to codeigni...@googlegroups.com, imog...@googlemail.com
Good work :-)

itei6 Test

unread,
Sep 16, 2016, 2:31:33 AM9/16/16
to CodeIgniter Kenya, imog...@googlemail.com
I have used this code in my project and configured as it has given instructions, but in my database table i am not getting any values stored for user activity.
any idea or or do i need to add more code to it.

melleji mollel

unread,
Jan 15, 2017, 4:43:44 PM1/15/17
to CodeIgniter Kenya, imog...@googlemail.com
thank you very much. this worked for me but you have to enable hook on application/config.php and set it to true $config['enable_hooks'] = TRUE;
Reply all
Reply to author
Forward
0 new messages