I'm creating a PHP-MySQL application for an insurance business that contains information of the insurances, accidents.
The application is not started yet but I need to log ALL THE ACTIVITIES one user does in the page, ALL of them, that means if the user modify an textfile, the DB save the date, hour, file and line the user modified.
I know how to do the base application, but im really confused in that activity log. Can you help me on it? I need to assign a variable to all the fields or something?
All the help is really appreciated. Thank you.
答案 0 :(得分:5)
如果您要将日志数据存储在数据库中,并且您正在使用某些MVC,则可以简单地覆盖每个控制器基类,如下所示:
class DefaultController extends LogController
{
public function update($objectId)
{
// do stuff like check POST values and update query
$this->log('Object with ID: '.$objectId.' has been updated successfuly', 1);
}
public function delete($objectId)
{
// mysql query to delete the object
$this->log('Object with ID: '.$objectId.' has been removed!', 1);
}
}
和LogController:
abstract class LogController extends BaseController // if you use any, just like Symfony does
{
public function log($message, $status)
{
$user = $_SESSION['userId']; // I assume you keep online user info somewhere
// get connection to a database and...
$connection->query('INSERT INTO logs (message, user_id, status) VALUES ("'.$message.'", '.$user.', '.$status.')');
}
}
但是如果你不使用MVC结构或任何其他面向对象的模板,那么就像这样做简单的日志功能:
function logAction($message, $status, $connection)
{
$user = $_SESSION['userId'];
$connection->query(/* insert query */);
}
我建议您将$连接传递给数据库的原因是,如果您只使用了一个连接,则不会打开新连接。如果已建立连接,您可以随时在logAction()
函数中进行简单检查,如果没有,则创建一个新函数。
修改强>
啊,忘了..这里有MySQL查询来创建日志表:
CREATE TABLE IF NOT EXISTS `logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`status` tinyint(1) NOT NULL,
`message` text NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
答案 1 :(得分:-1)
在数据库中创建log
表。对于您在系统中执行的每个操作,然后将记录插入log
表中,并引用活动,时间戳和用户名。