Errors when calling exit() function for fastCGI?

冷暖自知 提交于 2019-12-08 05:26:54

问题


I've been reading that people face problems when using the exit function in their php script while running fastCGI

https://serverfault.com/questions/84962/php-via-fastcgi-terminated-by-calling-exit

http://php.net/manual/en/function.exit.php

"It should be noted that if building a site that runs on FastCGI, calling exit will generate an error in the server's log file. This can quickly fill up."

However my error log isn't reporting this problem after running this simple script even though I have fastCGI configured:

<?php
$num=2;

if($num==2){
    exit();
}
?>

Would it be safe to use the exit function while I have fastCGI configured? And are there any alternatives to the exit function in php?

EDIT: I'm using the exit() function form form validation (ie if a form is valid exit, if not parse all the posted variables into the text fields.)


回答1:


There are a few legitimate reasons to use exit() which is the same as die(). One example would be to follow a header Location: redirect.

Form validation is not the place to use die(). Structure your code so that you utilize functions or classes with methods that return values instead, and utilize branching logic.

In terms of fastcgi, if exit is used appropriately for situations where code is reached that should not be, then those situations will be atypical and a few log messages should not be a problem. Having a log file fill up seems a pretty silly reason not to do something -- an active webserver is logging every request and nobody argues that you shouldn't have a web log.




回答2:


There's a really nice alternative to exit() posted on the exit() man page by "dexen dot devries at gmail dot com":

If you want to avoid calling exit() in FastCGI as per the comments below, but really, positively want to exit cleanly from nested function call or include, consider doing it the Python way:

define an exception named `SystemExit', throw it instead of calling exit() and catch it in index.php with an empty handler to finish script execution cleanly.

<?php

// file: index.php
class SystemExit extends Exception {}
try {
   /* code code */
}
catch (SystemExit $e) { /* do nothing */ }
// end of file: index.php

// some deeply nested function or .php file    

if (SOME_EXIT_CONDITION)
   throw new SystemExit(); // instead of exit()

?>


来源:https://stackoverflow.com/questions/6805474/errors-when-calling-exit-function-for-fastcgi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!