如何使用 php 和 mysql 记录用户操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2488267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:40:48  来源:igfitidea点击:

How to log user actions with php and mysql?

phpmysqlandroid-activitylogging

提问by eGGzy

I'm working on my CMS and I want it to log activities by users and other admins. For example: when new user registers or admin makes a new news post -> update last activity.

我正在处理我的 CMS,我希望它记录用户和其他管理员的活动。例如:当新用户注册或管理员发布新新闻时 -> 更新上次活动。

I want to know what is the best and easiest way.

我想知道什么是最好和最简单的方法。

回答by Peter Hahndorf

  • Create a table in your database to log your user activity.
  • Define the various activity types that can happen in your App.
  • Create a common function that logs any activity to that table.
  • Call that function from anywhere you perform log-worthy activities in your app.
  • 在您的数据库中创建一个表来记录您的用户活动。
  • 定义您的应用程序中可能发生的各种活动类型。
  • 创建一个通用函数,将任何活动记录到该表中。
  • 从您在应用程序中执行值得记录的活动的任何位置调用该函数。

You can then write a reporting tool that gives your admins access to those logged activities, you can filter by user, time and activity types.

然后,您可以编写一个报告工具,让您的管理员可以访问这些记录的活动,您可以按用户、时间和活动类型进行过滤。

In my log-framework, I specially mark activities which could be seen as malicious actions and assign them different numeric threat-values. If the sum of a user's thread-value reaches a certain threshold I log-out the user.

在我的日志框架中,我特别标记了可能被视为恶意行为的活动,并为它们分配不同的数字威胁值。如果用户的线程值总和达到某个阈值,我将注销该用户。

Ideally if you write an Application, you write your infrastructure code like logging at the very beginning and then use it in all your business logic code later.

理想情况下,如果您编写应用程序,您应该在最开始编写基础设施代码,例如日志记录,然后在以后的所有业务逻辑代码中使用它。

Edit for cleanup:

编辑清理:

Over time you may collect lots of records in that table. Depending on your requirements you could do different things.

随着时间的推移,您可能会在该表中收集大量记录。根据您的要求,您可以做不同的事情。

  • Delete any entries older than x days (maybe a year)

  • Delete any entries of certain types older than x days, but keep entries of other types for longer, or forever.

  • Move entries older than a certain threshold into an archive log table. This keeps your main table small but allows you to access older log data if you really have to. I have a checkbox Use archiveon my review logs page.

  • 删除任何超过 x 天(可能是一年)的条目

  • 删除任何早于 x 天的某些类型的条目,但将其他类型的条目保留更长时间或永久。

  • 将超过某个阈值的条目移动到存档日志表中。这使您的主表变小,但允许您在确实需要时访问较旧的日志数据。Use archive我的评论日志页面上有一个复选框。

回答by Avi Flax

Basic Answer

基本答案

Instead of doing this yourself, from scratch, check out how some existing systems do it, and if their license allows, use their design and code (make sure you document what code you've used and add a copyright notice to your CMS somewhere).

不要自己做,从头开始,检查一些现有系统是如何做的,如果他们的许可允许,使用他们的设计和代码(确保你记录你使用的代码,并在你的 CMS 某处添加版权声明) .

Possibly Helpful Example

可能有用的例子

I'm not sure about PHP CMS's which do this, but I know Django'sadmin app does. Django is implemented in Python, but it should be fairly straightforward to port this code over to PHP. Even if the code isn't a straight port, the design could be ported.

我不确定执行此操作的 PHP CMS,但我知道Django 的管理应用程序可以。Django 是用 Python 实现的,但将此代码移植到 PHP 应该相当简单。即使代码不是直接移植,也可以移植设计。

The file which contains the logging is in the adminmodule in models.py.

包含日志的文件admin位于models.py的模块中。

Some key aspects:

一些关键方面:

The data model for the logging table:

日志表的数据模型:

class LogEntry(models.Model):
  action_time = models.DateTimeField(_('action time'), auto_now=True)
  user = models.ForeignKey(User)
  content_type = models.ForeignKey(ContentType, blank=True, null=True)
  object_id = models.TextField(_('object id'), blank=True, null=True)
  object_repr = models.CharField(_('object repr'), max_length=200)
  action_flag = models.PositiveSmallIntegerField(_('action flag'))
  change_message = models.TextField(_('change message'), blank=True)
  objects = LogEntryManager()

And the LogEntryManager, which saves the actual log entries:

以及保存实际日志条目的 LogEntryManager:

class LogEntryManager(models.Manager):
  def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
    e = self.model(None, None, user_id, content_type_id, smart_unicode(object_id), object_repr[:200], action_flag, change_message)
    e.save()

HTH, good luck!

HTH,祝你好运!

回答by Syntax Error

I use two tables for activities, one that gives each activity an id, and another one that just logs the user id, activity id, and a timestamp. I do this because an int takes up less space than a string, so why log the same strings over and over? The second one isn't really necessary, you just just as easily keep the action codes in a text file for your own reference, but the db just seems like a easier place to remember.

我为活动使用两个表,一个为每个活动提供一个 ID,另一个只记录用户 ID、活动 ID 和时间戳。我这样做是因为 int 占用的空间比字符串少,那么为什么要一遍又一遍地记录相同的字符串呢?第二个不是真正必要的,您只需将操作代码保存在文本文件中以供您自己参考即可,但数据库似乎更容易记住。

In the past I've used a function to handle the actual logging actions, but the next time I do it I'm going to be using the Observer pattern. It appears to be a lot more flexible, and I've already had to edit out logging function calls from older code I have that wasn't going to log anything. I much prefer reusing code with no editing required.

过去我使用一个函数来处理实际的日志记录操作,但下次我将使用观察者模式。它似乎更灵活,而且我已经不得不从旧代码中编辑掉日志函数调用,我不会记录任何内容。我更喜欢无需编辑即可重用代码。

回答by Shailender Ahuja

Its very simple to do with PHP/JAVA FUNCTIONJQUERYand its AJAXdata posting method... Before posting the solution -- Lets read these two lines

使用PHP/JAVA FUNCTION JQUERY及其AJAX数据发布方法非常简单......在发布解决方案之前 - 让我们阅读这两行

Why and What we want to record ? --- As we know only to record transaction with in the database --not all the clicks and checks -- but yes its possible with this solution....

为什么以及我们想要记录什么?--- 正如我们所知道的,只在数据库中记录交易——不是所有的点击和检查——但是这个解决方案是可能的....

Here is the solution step by step: -

这是一步一步的解决方案: -

1. create a DB Table -- to record these things
     a) Page Name.
     b) logged in user name
     c) session details (To record all the sessions).
     d) POST/GET data details (To record all the post/get data for the       
        page)
     e) Record Created Date.

or any other thing that you want to record. 2. Create a Jquery function or PHP function -- which will be auto triggered with every page. 3. This function will collect all the session of that page,user logged in details and what data passed to that page. Apart from this - you can also record -- from which page this new page is called -- Its pretty simple and best way to implement loggs recording features even in already running old software's :)

或您想记录的任何其他内容。2. 创建一个 Jquery 函数或 PHP 函数——每个页面都会自动触发。3. 该函数将收集该页面的所有会话,用户登录的详细信息以及传递给该页面的数据。除此之外 - 您还可以记录 - 从哪个页面调用这个新页面 - 即使在已经运行旧软件的情况下,它也是实现日志记录功能的非常简单和最好的方法:)

If you want all the Code i mentioned above to use -- Search it over the NET the mechanism i have defined just you need FUNCTION CODE -- AUTO execute function code -- simple

如果您希望使用我上面提到的所有代码 - 在 NET 上搜索我定义的机制,您只需要功能代码 - 自动执行功能代码 - 简单