php ini_set('max_execution_time', 0) 是个坏主意吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4306605/
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 12:36:39  来源:igfitidea点击:

Is ini_set('max_execution_time', 0) a bad idea?

php

提问by benizi

Is there a good reason not to set the PHP configuration variable max_execution_timeto 0?

是否有充分的理由不将 PHP 配置变量设置max_execution_time为 0?

A coworker recently checked in a change to a file that added:

一位同事最近签入了对文件的更改,其中添加了:

ini_set('max_execution_time', 0);

The default value had been too low for a page that did some complex processing before returning the output to the user.

对于在将输出返回给用户之前进行一些复杂处理的页面来说,默认值太低了。

The manual states that the main purpose of the setting is to:

手册指出,设置的主要目的是:

prevent poorly written scripts from tying up the server.

防止写得不好的脚本占用服务器。

But also goes on to state:

但也继续说明:

Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeoutdirective and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.

您的 Web 服务器可能有其他超时配置,这些配置也可能会中断 PHP 执行。Apache 有一个Timeout指令,IIS 有一个 CGI 超时功能。两者都默认为 300 秒。有关特定详细信息,请参阅您的 Web 服务器文档。

We're running under Apache, so that Timeoutsetting applies. Is there any reason not to set max_execution_timeto zero globally? I'm mainly curious as to whether there are benefits I'm overlooking when notsetting it to zero.

我们在 Apache 下运行,因此超时设置适用。有什么理由不max_execution_time全局设置为零吗?我主要想知道在将其设置为零时是否有我忽略的好处。

回答by Teekin

At the risk of irritating you;

冒着激怒你的风险;

You're asking the wrong question. You don't need a reason NOT to deviate from the defaults, but the other way around. You need a reason to do so. Timeouts are absolutely essential when running a web server and to disable that setting without a reason is inherently contrary to good practice, even if it's running on a web server that happens to have a timeout directive of its own.

你问错了问题。您不需要理由不偏离默认值,而是相反。你需要一个理由这样做。在运行 Web 服务器时,超时是绝对必要的,并且无故禁用该设置本质上与良好实践背道而驰,即使它运行在恰好具有自己的超时指令的 Web 服务器上。

Now, as for the real answer; probably it doesn't matter at all in this particular case, but it's bad practice to go by the setting of a separate system. What if the script is later run on a different server with a different timeout? If you can safely say that it will never happen, fine, but good practice is largely about accounting for seemingly unlikely events and not unnecessarily tying together the settings and functionality of completely different systems. The dismissal of such principles is responsible for a lot of pointless incompatibilities in the software world. Almost every time, they are unforeseen.

现在,至于真正的答案;在这种特殊情况下可能根本无关紧要,但是通过设置单独的系统进行操作是不好的做法。如果脚本稍后在具有不同超时的不同服务器上运行怎么办?如果您可以有把握地说它永远不会发生,那很好,但是好的做法主要是考虑到看似不太可能发生的事件,而不是将完全不同系统的设置和功能不必要地联系在一起。对这些原则的摒弃是软件世界中许多毫无意义的不兼容性的原因。几乎每一次,他们都是不可预见的。

What if the web server later is set to run some other runtime environment which only inherits the timeout setting from the web server? Let's say for instance that you later need a 15-year-old CGI program written in C++ by someone who moved to a different continent, that has no idea of any timeout except the web server's. That might result in the timeout needing to be changed and because PHP is pointlessly relying on the web server's timeout instead of its own, that may cause problems for the PHP script. Or the other way around, that you need a lesser web server timeout for some reason, but PHP still needs to have it higher.

如果稍后将 Web 服务器设置为运行其他仅从 Web 服务器继承超时设置的运行时环境,该怎么办?例如,假设您后来需要一个由搬到不同大陆的人用 C++ 编写的 15 年历史的 CGI 程序,除了 Web 服务器的超时之外,他不知道任何超时。这可能导致需要更改超时,并且因为 PHP 毫无意义地依赖 Web 服务器的超时而不是它自己的超时,这可能会导致 PHP 脚本出现问题。或者反过来说,出于某种原因,您需要较小的 Web 服务器超时,但 PHP 仍然需要将其设置得更高。

It's just not a good idea to tie the PHP functionality to the web server because the web server and PHP are responsible for different roles and should be kept as functionally separate as possible. When the PHP side needs more processing time, it should be a setting in PHP simply because it's relevant to PHP, not necessarily everything else on the web server.

将 PHP 功能绑定到 Web 服务器并不是一个好主意,因为 Web 服务器和 PHP 负责不同的角色,并且应该在功能上尽可能保持独立。当 PHP 端需要更多处理时间时,它应该是 PHP 中的一个设置,因为它与 PHP 相关,而不一定是 Web 服务器上的其他所有内容。

In short, it's just unnecessarily conflating the matter when there is no need to.

简而言之,它只是在没有必要的时候不必要地混淆了这个问题。

Last but not least, 'stillstanding' is right; you should at least rather use set_time_limit()than ini_set().

最后但并非最不重要的一点是,“静止不动”是正确的;你至少应该使用而set_time_limit()不是ini_set().

Hope this wasn't too patronizing and irritating. Like I said, probably it's fine under your specific circumstances, but it's good practice to not assume your circumstances to be the One True Circumstance. That's all. :)

希望这不会太傲慢和刺激。就像我说的,在你的特定情况下可能没问题,但最好不要假设你的情况是唯一的真实情况。就这样。:)

回答by HelloDolly1

Reason is to have some value other than zero. General practice to have it short globally and long for long working scripts like parsers, crawlers, dumpers, exporting & importing scripts etc.

原因是具有除零以外的其他值。一般做法是在全局范围内缩短它,而对于长时间工作的脚本(如解析器、爬虫、转储程序、导出和导入脚本等)来说则更长。

  1. You can halt server, corrupt work of other people by memory consuming script without even knowing it.
  2. You will not be seeing mistakes where something, let's say, infinite loop happened, and it will be harder to diagnose.
  3. Such site may be easily DoSed by single user, when requesting pages with long execution time
  1. 您可以在不知情的情况下通过消耗内存的脚本停止服务器,破坏其他人的工作。
  2. 你不会在某些事情上看到错误,比如说,发生了无限循环,而且诊断起来会更困难。
  3. 当请求执行时间较长的页面时,此类站点可能很容易被单个用户拒绝