SetBased/php-audit

Audit_usr_id and Session

zeroappz opened this issue · 1 comments

I have set the variable '@abc_g_usr_id' at the time of user logon in my php application. I tried the static variable in audit.json file in additional sql column like the below
"set @'abc_g_usr_id' = 4;"
and it has been reflected in db table properly. Now, I got stuck in setting up the variable in login page. Can you help me out how can we use the variable for auditing End user and Session. I read the document but unable to find the solution.

I have made some changes to the documentation. Please, consider reading sections https://php-audit.readthedocs.io/en/latest/audit-config-file.html#example-8-end-user and https://php-audit.readthedocs.io/en/latest/miscellaneous.html#setting-user-defined-variables-in-mysql again.

For recording the user who has made data changes in your application we must somehow pass the ID of the user (in the documentation we assume the user ID is stored in column usr_id of the user table) in PHP to the triggers in MySQL. To achieve this we use user defined variables in MySQL (in MySQL user defined variables start with @).

You can set a user defined variable in MySQL from PHP with like this (where $mysql is a mysqli object connected to the MySQL server):

$mysql->real_query(sprintf('set @audit_usr_id = %s', $usrId ?? 'null'));

You must do this after retrieving the session (for each HTTP request) and directly after signing in and out of the end user.

If you don't succeeded, don't hesitate to contact me ad send you config file as well.

BTW:
The single quotes in set @'abc_g_usr_id' = 4; are not required.