简体   繁体   English

php脚本调用“ exit”对性能有何影响?

[英]What are the performance implications of a php script calling 'exit'?

I've noticed many times where some php scripts exit. 我注意到很多时候退出了一些php脚本。 It seems to me that this will force an exit of the httpd/apache child (of course another will be started if required for the next request). 在我看来,这将强制退出httpd / apache子级(当然,如果下一个请求需要,则将启动另一个子级)。

But in the CMS, that next request will require the entire init.php initialization, and of course just cleaning up and starting php in the first place. 但是在CMS中,下一个请求将需要整个init.php初始化,并且当然首先要清理并启动php。

It seems that the php files usually start with 似乎php文件通常以

if ( !defined( 'SMARTY_DIR' ) ) {
include_once( 'init.php' );
}

which suggests that somebody was imagining that one php process would serve multiple requests. 这表明有人在想一个php进程可以处理多个请求。 But if every script exits, then each php/apache process will serve one request only. 但是,如果每个脚本均退出,则每个php / apache进程将仅处理一个请求。

Any thoughts on the performance and security implications of removing many of the exit calls (especially from the most-frequently-called scripts like index.php etc) to allow one process to serve multiple requests? 关于删除许多出口调用(特别是从最常用的脚本(如index.php等)中删除)以允许一个进程处理多个请求的性能和安全性有何想法​​?

Thanks, Peter 谢谢彼得

--ADDENDUM -- -附录-

Thank you for the answers. 谢谢你的回答。 That (php will never serve more than one request) is what I thought originally until last week, when I was debugging a config variable that could only have been set in one script (because of the way the path was set up) but was still set in another script (this is on a webserver with about 20 hits/sec). 那(PHP永远不会满足一个以上的请求)是我上周一直想的,当时我正在调试一个只能在一个脚本中设置的配置变量(由于路径的设置方式),但仍然在另一个脚本中设置(该脚本位于Web服务器上,命中率为20左右/秒)。 In that case, I did not have a php exit call in the one script that set up its config slightly differently. 在那种情况下,我在一个脚本中没有php出口调用,该脚本将其配置略有不同。 But when I added the php exit call to that one script (in the alternate directory) this solved the misconfiguration I was experiencing in all my main scripts in the main directory (which were due to having a css directory variable set erroneously, in a previous page execution). 但是,当我向该脚本(在备用目录中)添加php exit调用时,这解决了我在主目录中所有主脚本中遇到的配置错误(这是由于在先前的版本中错误地设置了css目录变量)页面执行)。 So now I'm confused again, because with what all the answers so far say, php should never serve more than one request. 所以现在我再次感到困惑,因为到目前为止,所有答案都表明,php应该永远不会满足多个请求。

exit does nothing to Apache processes (it certainly doesn't kill a worker!). exit对Apache进程没有任何作用(它肯定不会杀死工作人员!)。 It simply ends the execution of the PHP script and returns execution to the Apache process, which'll send the results to the browser and continue on to the next request. 它只是简单地结束了PHP脚本的执行并将执行返回 Apache进程,该进程会将结果发送到浏览器并继续下一个请求。

The Smarty code you've excerpted doesn't have anything to do with a PHP process serving multiple requests. 您摘录的Smarty代码与处理多个请求的PHP流程没有任何关系。 It just insures that Smarty is initialised at all times - useful if a PHP script might be alternatively included in another script or accessed directly. 它只是确保Smarty始终处于初始化状态-如果PHP脚本可以替换地包含在另一个脚本中或直接访问,则很有用。

I think your confusion comes from what include_once is for. 我认为您的困惑来自include_once的用途。 PHP is basically a "shared-nothing" system, where there are no real persistent server objects. PHP基本上是一个“无共享”系统,其中没有真正的持久服务器对象。 include_once doesn't mean once per Apache child, but once per web request. include_once不是每个Apache子include_once一次,而是每个Web请求一次。

PHP may hack up a hairball if you include the same file twice. 如果您两次包含相同的文件,则PHP可能会破坏一个范围。 For instance a function with a particular name can only be defined once. 例如,具有特定名称的功能只能定义一次。 This led to people implementing a copy of the old C #ifndef-#define-#include idiom once for each included file. 这导致人们为每个包含的文件实施一次旧的C#ifndef-#define-#include惯用语的副本。 include_once was the fix for this. include_once是解决此问题的方法。

Even if you do not call exit your PHP script is still going to end execution, at which point any generated HTML will be returned to the web server to send on to your browser. 即使您不调用exit,您的PHP脚本仍将结束执行,这时任何生成的HTML都将返回到Web服务器以发送到浏览器。

The exit keyword allows you to signal to the PHP engine that your work is done and no further processing needs to take place. exit关键字使您可以向PHP引擎发送信号,表明您的工作已经完成,不需要进行进一步的处理。

Also note that exit is typically used for error handling and flow control - removing it from includes will likely break your application. 还要注意,退出通常用于错误处理和流控制-从include中删除它可能会破坏您的应用程序。

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

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