Фоновое выполнение скрипта на PHP без crontab. Фоновое выполнение скрипта на PHP без crontab Php как остановить выполнение скрипта

8 years ago

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.

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

?>

10 years ago

Jbezorg at gmail proposed the following:


header ("Location: /" );

?>

After sending the `Location:" header PHP _will_ continue parsing, and all code below the header() call will still be executed. So instead use:

If($_SERVER [ "SCRIPT_FILENAME" ] == __FILE__ )
{
header ("Location: /" );
exit;
}

?>

10 years ago

To rich dot lovely at klikzltd dot co dot uk:

Using a "@" before header() to suppress its error, and relying on the "headers already sent" error seems to me a very bad idea while building any serious website.

This is *not* a clean way to prevent a file from being called directly. At least this is not a secure method, as you rely on the presence of an exception sent by the parser at runtime.

I recommend using a more common way as defining a constant or assigning a variable with any value, and checking for its presence in the included script, like:

in index.php:


in your included file:


BR.

4 years ago

A side-note for the use of exit with finally: if you exit somewhere in a try block, the finally won"t be executed. Could not sound obvious: for instance in Java you never issue an exit, at least a return in your controller; in PHP instead you could find yourself exiting from a controller method (e.g. in case you issue a redirect).

Here follows the POC:



This will print:

testing finally wit exit
In try, exiting

1 year ago

>> Shutdown functions and object destructors will always be executed even if exit is called.

It is false if you call exit into desctructor.

Normal exit:


// Exit into desctructor:

17 years ago

If you are using templates with numerous includes then exit() will end you script and your template will not complete (no , , etc...). Rather than having complex nested conditional logic within your content, just create a "footer.php" file that closes all of your HTML and if you want to exit out of a script just include() the footer before you exit().

include ("header.php");
blah blah blah
if (!$mysql_connect) {
echo "unable to connect";
include ("footer.php");
exit;
}
blah blah blah
include ("footer.php");

16 years ago

Return may be preferable to exit in certain situations, especially when dealing with the PHP binary and the shell.

I have a script which is the recipient of a mail alias, i.e. mail sent to that alias is piped to the script instead of being delivered to a mailbox. Using exit in this script resulted in the sender of the email getting a delivery failure notice. This was not the desired behavior, I wanted to silently discard messages which did not satisfy the script"s requirements.

After several hours of trying to figure out what integer value I should pass to exit() to satisfy sendmail, I tried using return instead of exit. Worked like a charm. Sendmail didn"t like exit but it was perfectly happy with return. So, if you"re running into trouble with exit and other system binaries, try using return instead.

3 years ago

In addition to "void a t informance d o t info", here"s a one-liner that requires no constant:



Placing it at the beginning of a PHP file will prevent direct access to the script.

To redirect to / instead of dying:



Doing the same in a one-liner:



A note to security: Even though $_SERVER["PHP_SELF"] comes from the user, it"s safe to assume its validity, as the "manipulation" takes place _before_ the actual file execution, meaning that the string _must_ have been valid enough to execute the file. Also, basename() is binary safe, so you can safely rely on this function.

7 years ago

When using php-fpm, fastcgi_finish_request() should be used instead of register_shutdown_function() and exit()

For example, under nginx and php-fpm 5.3+, this will make browsers wait 10 seconds to show output:

 
Статьи по теме:
Как разблокировать телефон
Как разблокировать от оператора ваш Мегафон Login 2 1. Вставляете сим-карту другого сотового оператора в телефон. 2. Включаете Мегафон Login 2 (Megafon Login 2 MS3A) . 3. Должно появится окно для ввода кода разблокировки . 4. Вводите код: 67587048 5. Теп
Asus ZenFone Max ZC550KL — Советы, рекомендации, часто задаваемые вопросы и полезные параметры
Как вставить SIM-карту на свой Asus ZenFone Max? Asus ZenFone Max — это смартфон с двумя SIM-картами и поддерживает соединение 2G / 3G / 4G. SIM-карта, поддерживаемая устройством, является Micro SIM-картой и может быть видна после снятия задней крышки тел
Что такое расширение файла CDR?
CDR-формат — это файл, который был создан в программе Corel DRAW, содержащей растровое или векторное изображение. Компания Corel использует этот формат в собственных продуктах, поэтому его можно открыть также другим программным обеспечением данной компани
Multisim 17 где находится библиотека элементов
Компоненты и библиотеки элементов Multisim 11 Контрольно-измерительные и индикаторные приборы В Multisim имеются измерительные приборы, каждый из которых можно использовать в схеме только один раз. Эти приборы рас­положены в библиотеке контрольно-из