user784637
user784637

Reputation: 16092

Errors when calling exit() function for fastCGI?

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.)

Upvotes: 1

Views: 1961

Answers (2)

foxy
foxy

Reputation: 7672

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()

?>

Upvotes: 2

gview
gview

Reputation: 15361

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.

Upvotes: 2

Related Questions