如何在PHP中记录对函数的所有调用? (mail()函数) [英] How to log all calls to a function in PHP? (mail() function)

查看:211
本文介绍了如何在PHP中记录对函数的所有调用? (mail()函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数十个虚拟主机的专用服务器。我想确定哪个文件正在调用mail()函数,并将其全局记录下来。我需要这样的东西:

I have a dedicated server with tens of virtual hosts. I want to determine what file is calling mail() function and log this globally. I need something like that:

[Wed Feb 13 10:42:39 2013] mail() called from /var/www/example1.php on line 70
[Wed Feb 13 10:42:40 2013] mail() called from /var/www/example2.php on line 70

我不能使用debug_backtrace()或类似的东西,因为我不能将其添加到服务器中的任何PHP文件中。我可以在文件中全局记录所有函数调用,例如将错误记录到error.log这样的文件中吗?

I can't use debug_backtrace() or similar because I can't add this to any PHP file in the server. Can I log all function calls globally in a file like errors are logged to a file like error.log?

谢谢

推荐答案

通常,您会遇到麻烦; PHP没有提供内置的日志记录机制,因此您需要在PHP系统中添加一些内容。

In general, you are going to have trouble with this; PHP doesn't provide a logging mechanism built-in, so you'll need to add something to your PHP system to do it.

我所看到的选项是:


  1. 修改PHP代码。鉴于您在问题中所说的话,我想这不是一个选择,但需要说明一下,因为这是显而易见的答案。 (PHP的 mail()函数是如此基础,以至于任何编写PHP代码的人都应该为此使用包装类,以保持理智)

  1. Modify the PHP code. Given what you've said in the question, I guess this isn't an option, but it needs to be stated, as it is the obvious answer. (PHP's mail() function is so basic that anyone writing PHP code really ought to be using a wrapper class for it anyway just to retain their sanity)

如果我们专门讨论 mail()函数,则可以通过记录sendmail客户端 mail()调用。您服务器上的 sendmail 系统可能由unix shell脚本控制,该脚本由PHP的 mail()函数调用。您应该能够修改此Shell脚本以记录邮件事件。这样可能无法为您提供行号之类的详细信息,但是它将告诉您正在执行的操作,等等。

If we're talking specifically about the mail() function, then we could log it by logging the sendmail client that mail() calls. Your sendmail system on the server is probably controlled by a unix shell script that is called by PHP's mail() function. You should be able to modify this shell script to log the mail events. This probably won't be able to give you details like the line number, but it will tell you the user that is doing it, etc.

使用 Suhosin PHP强化补丁。这是一个PHP修补程序,提供了许多与安全性相关的功能;无论如何,我强烈建议将其用于共享主机环境。但对您来说,它还包括日志记录功能,它可能使您可以登录特定功能的用法,包括文件名和行号-即正是您要查找的方案。这是我推荐的解决方案....这里唯一的大问题是,如果您保持PHP版本最新,因为Suhosin当前仅适用于PHP 5.3,而不适用于5.4。作为一个PHP开发人员,我将努力在服务器上获得PHP 5.4,但是作为提供程序,仍然支持5.3,因此它没有任何问题。 (相反,如果您仍然使用5.2,则应尽快升级,因为多年来一直不受支持,并且已知安全漏洞。)

Use the Suhosin PHP hardening patch. This is a patch for PHP that provides dozens of security-related features; I would strongly recommend it for a shared hosting environment anyway. But for you it also includes logging features, which may allow you to log usage of particular functions, including filename and line number -- ie exactly the scenario you're looking for. This is the solution I'd recommend .... the only big problem you'll have here is if you've kept your PHP version bang up to date, because Suhosin is currently only available for PHP 5.3, not 5.4. As a PHP developer, I would be pushing to get PHP 5.4 on my server, but as a provider, 5.3 is still supported, so there's nothing wrong with it. (on the flip side, if you are still on 5.2 you should be upgrading ASAP, as it's been unsupported for years and has known security holes).

这篇关于如何在PHP中记录对函数的所有调用? (mail()函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆