So you want to log all table access

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

So you want to log all table access

Post by shasta59 » 2013-05-21 13:42

Hello Again

You have decided you wish to log all access to every table in your database. You have tried the method as posted by Ahmad on the main site but it will not work or you just get a blank page each time.

Here is the link on the AppGini site:
http://www.bigprof.com/appgini/help/adv ... obal-hooks

Here is the code from that link:

function login_ok($memberInfo, &$args){
// the log file where we'll save member activity
$logFile='members.log';

// the member details we'll be saving into the file
$username=$memberInfo['username'];
$ip=$memberInfo['IP'];
$date=date('m/d/Y');
$time=date('h:i:s a');

// open the log file and append member login details
if(!$fp=@fopen($logFile, 'a')) return '';
fwrite($fp, "$date,$time,$username,$ip\n");
fclose($fp);

return '';
}


But it will just not work for you. Simple fix. Remove the last return ''; and past it into yourtablename.php in hooks folder in the yourtablename_init function so it looks like this now. (Make sure you have a unique file name for your log file).

Code: Select all

function yourtablename_init(&$options, $memberInfo, &$args){
		//the log file where we'll save member activity
	//added to create log file
	$memberInfo = getMemberInfo();
	$logFile=uniquename.log'; //this can also be a .txt file if you wish

	// the member details we'll be saving into the file
	$username=$memberInfo['username'];
	$ip=$memberInfo['IP'];
	$date=date('m/d/Y');
	$time=date('h:i:s a');

	// open the log file and append member login details
	if(!$fp=@fopen($logFile, 'a')) return '';
	fwrite($fp, "$date,$time,$username,$ip\n");
	fclose($fp);
	
	//end of log file capture
	
		return TRUE;
	}
While a minor change it can cause issues if you are not aware of changing the return line. Also you can add in any details you wish to the log file by pulling the info from any other table you have by doing a mysql_query.

One thing I do with this data is I save it to a table in my database where it records username and other info. This way I can check to see who is looking at what to check to see if the table is being used or not. I also write to another table when a entry is changed and by whom to insure I can track who is making changes to data in tables. This gives me an ongoing record of changes, when and by whom. I do not track what change was made.

This is also done by putting code in the hook file in the function yourtablename_after_update($data, $memberInfo, &$args) area of the hook file. I create a table in AppGini with the fields I wish to record and then write my own code to update it. I turn off the ability for anyone to create a new record in that table and access is very limited. It records user name, date, time, table changed and other tidbits. I am also working on writing a routine to record what was changed. This is just a matter of putting into an array the existing record data then running a comparison when the record is updated between what is in the array of existing data and what is different then recording that info into the same table.

Hope this helps someone.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

User avatar
toconnell
Veteran Member
Posts: 204
Joined: 2013-04-09 19:29
Location: Oklahoma City, OK
Contact:

Re: So you want to log all table access

Post by toconnell » 2013-11-19 19:20

Alan,

Trying to dummy proof my app and I would love to do the table that showed any changes to any records or any new records added to any table. I would like to show, field name, changed from, changed to, user and date and time.
Do you have code you did and where you put it to create this?
It would really help me kick start this effort.

Thanks,
Tina
Tina O'Connell
Web Dev & Appgini FAN

Post Reply