简体   繁体   English

如何在PHP中记录对函数的所有调用? (mail()函数)

[英]How to log all calls to a function in PHP? (mail() function)

I have a dedicated server with tens of virtual hosts. 我有一台带有数十个虚拟主机的专用服务器。 I want to determine what file is calling mail() function and log this globally. 我想确定哪个文件正在调用mail()函数并将其全局记录。 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

I can't use debug_backtrace() or similar because I can't add this to any PHP file in the server. 我不能使用debug_backtrace()或类似的东西,因为我不能将其添加到服务器中的任何PHP文件中。 Can I log all function calls globally in a file like errors are logged to a file like error.log? 是否可以将所有函数调用全局记录在文件中,例如将错误记录到error.log这样的文件中?

Thanks 谢谢

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. PHP没有提供内置的日志记录机制,因此您需要在PHP系统中添加一些内容。

The options as I see it: 我看到的选项:

  1. Modify the PHP code. 修改PHP代码。 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) (PHP的mail()函数是如此基本,以至于任何编写PHP代码的人都应该为此使用包装类,以保持理智)

  2. If we're talking specifically about the mail() function, then we could log it by logging the sendmail client that mail() calls. 如果我们专门讨论mail()函数,则可以通过记录mail()调用的sendmail客户端来记录它。 Your sendmail system on the server is probably controlled by a unix shell script that is called by PHP's mail() function. 服务器上的sendmail系统可能是由unix shell脚本控制的,该脚本由PHP的mail()函数调用。 You should be able to modify this shell script to log the mail events. 您应该能够修改此Shell脚本以记录邮件事件。 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. 这可能无法为您提供行号之类的详细信息,但会告诉您用户正在执行操作等。

  3. Use the Suhosin PHP hardening patch . 使用Suhosin PHP强化补丁 This is a patch for PHP that provides dozens of security-related features; 这是一个PHP修补程序,提供了许多与安全性相关的功能。 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. 这是我建议的解决方案....这里唯一的大问题是,如果您保持PHP版本最新,因为Suhosin当前仅适用于PHP 5.3,而不适用于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. 作为一个PHP开发人员,我将努力在服务器上获得PHP 5.4,但是作为提供程序,仍然支持5.3,因此它没有任何问题。 (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). (另一方面,如果您仍在5.2上,则应尽快升级,因为它多年来一直不受支持并且已知安全漏洞)。

As of >= PHP 5.3.0, you can simply specify a path to the desired location of your logilfe in your php.ini : 从> = PHP 5.3.0开始,您只需在php.ini指定指向logilfe所需位置的路径即可:

mail.log = /path/to/some/logfile

See the PHP docs for details. 有关详细信息,请参见PHP文档

What you can do is, download php source code, edit this file: 您可以做的是,下载php源代码,编辑此文件:

ext/standard/mail.c

and add logger there. 并在其中添加记录器。

Then recompile. 然后重新编译。

That's probably only way you can implicitly debug who's calling your php function from where. 那可能是您可以隐式调试谁从哪里调用php函数的唯一方法。

要阻止mail(),只需从php的chroot删除/ usr / sbin / sendmail并强制通过身份验证的smtp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM