i use this code when i want php infinite loop
<?php
set_time_limit (0);//run script forever
ignore_user_abort ();//run script in background
$i = 0;
echo "start\n";
while (1) {
$i++;
echo $i, "\n";
$sleep = sleep (3);
if ($sleep == 0 or $sleep or $sleep == FALSE) continue;
if (connection_aborted ()) continue;
if (connection_status () != 0) continue;
}
?>
Bağlantı yönetimi
PHP içinde yerleşik olarak bağlantı durumu saklanır. Olası 3 durum vardır:
- 0 - NORMAL
- 1 - ABORTED
- 2 - TIMEOUT
PHP betiği normal olarak çalıştığında, NORMAL durumu etkindir. Eğer uzak istemci bağlantıyı keserse ABORTED durumu etkin olur. Uzak istemci bağlantı kesilmesi genellikle kullanıcının DUR düğmesine basmasından kaynaklanır. Eğer PHP tarafından bir zaman sınırı (set_time_limit() işlevine bakınız) tetiklendiyse, TIMEOUT durumu etkin olur.
Kullanıcının bağlantı kesmesiyle betiğinizin iptal edilip edilmeyeceğine
karar verebilirsiniz. Bazen uzak tarayıcı çıktıyı almasa da betiğinizin
işini bitirinceye kadar çalışmasını gerektiren durumlar olabilir. Öntanımlı
davranış uzak istemci bağlantıyı kestiğinde betik çalışmasının iptal
edilmesidir. Bu davranış şekli ignore_user_abort php.ini yönergesi veya ona ilişkin
php_value ignore_user_abort Apache yapılandırma
yönergesi veya ignore_user_abort() işlevi ile
belirlenebilir. Eğer PHP'ye kullanıcı iptallerini gözardı etmesini
söylemezseniz ve kullanıcı iptal ederse betiğiniz sonlanır. Tek istisnası
register_shutdown_function() işleviyle kapatma işlevinin
kayıtlanmasıdır. Bir kapatma işlevi ile, uzak kullanıcı DUR düğmesine
bastığında, betiğinizin sonraki çıktılama denemesinde PHP bağlantının iptal
edildiğini tespit eder ve kapatma işlevi çağrılır. Bu kapatma işlevi ayrıca
normal olarak sonlandırmada betiğinizin sonunda çağrılacaktır, istemci
tarafından bağlantı iptalinde farklı birşey yapmak için
connection_aborted() işlevini kullanabilirsiniz. Eğer
bağlantı iptal edilirse bu işlev TRUE döndürecektir.
Betiğinizi yerleşik betik zamanlayıcı ile de sonlandırabilirsiniz.
Öntanımlı zaman aşımı 30 saniyedir. Bu max_execution_time
php.ini yönergesi veya ilişkili php_value
max_execution_time Apache yapılandırma yönergesi veya
set_time_limit() işlevi ile değiştirilebilir. Zaman
aşımında betik iptal edilir ve yukarıdaki istemci bağlantı kesilmesindeki
gibi, eğer kayıtlı bir kapatma işlevi varsa çağrılır. Bu kapatma işlevi
içinde connection_status() işlevi ile kapatma işlevinin
zaman aşımı nedeniyle mi çağrıldığını denetleyebilirsiniz. Kapatma
işlevinin çağrılmasına zaman aşımı neden olduysa bu işlev 2 döndürür.
ABORTED ve TIMEOUT durumlarının aynı zamanda etkin olabileceğine dikkat edilmesi gerekir. Eğer PHP'ye kullanıcı iptallerini gözardı etmesini söylediyseniz bu mümkündür. PHP kullanıcının bağlantıyı koparmış olabileceğini bilir, fakat betik çalışmaya devam eder. Eğer zaman sınırına ulaşırsa çalışması iptal edilir ve kapatma işleviniz varsa çağrılır. Bu noktada connection_status() işlevinin 3 döndürdüğünü göreceksiniz.
Bağlantı yönetimi
05-Jan-2009 11:59
02-Apr-2008 12:25
connection_status() return ABORTED state ONLY if the client disconnects gracefully (with STOP button). In this case the browser send the RST TCP packet that notify PHP the connection is closed.
But.... If the connection is stopped by networs troubles (wifi link down by exemple) the script doesn't know that the client is disconnected :(
I've tried to use fopen("php://output") with stream_select() on writting to detect write locks (due to full buffer) but php give me this error : "cannot represent a stream of type Output as a select()able descriptor"
So I don't know how to detect correctly network trouble connection...
13-Nov-2007 12:06
in regards of posting from:
arr1 at hotmail dot co dot uk
if you use/write sessions you need to do this before:
(otherwise it does not work)
session_write_close();
and if wanted:
ignore_user_abort(TRUE);
instead of ignore_user_abort();
14-Nov-2006 09:51
Closing the users browser connection whilst keeping your php script running has been an issue since 4.1, when the behaviour of register_shutdown_function() was modified so that it would not automatically close the users connection.
sts at mail dot xubion dot hu
Posted the original solution:
<?php
header("Connection: close");
ob_start();
phpinfo();
$size=ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
sleep(13);
error_log("do something in the background");
?>
Which works fine until you substitute phpinfo() for
echo ('text I want user to see'); in which case the headers are never sent!
The solution is to explicitly turn off output buffering and clear the buffer prior to sending your header information.
example:
<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
echo ('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here
sleep(30);
echo('Text user will never see');
?>
Just spent 3 hours trying to figure this one out, hope it helps someone :)
Tested in:
IE 7.5730.11
Mozilla Firefox 1.81
22-Sep-2005 04:42
Confirmed. User presses STOP button. This sends a RST packet and closes the connection. PHP is most certainly immediately affected (i.e., the script is stopped, whether or not any output is pending for the user, or even if script is just grinding away on a database without having output anything).
ignore_user_abort() exists to prevent this.
If user STOPS, script ignores the RST and runs to completion (the output is apparently ignored by apache and not sent to the user, who sent the RST and closed the TCP connection). If user's connection just vanishes (isp problem, disconnect, whatever), and there is no RST sent by user, then eventually the script will timeout.
12-Dec-2004 09:08
As it was said, connection handling is very useful when web application need to do something in background. I found it very useful when application need something from database, wrap that data with template, create some html files and save it to filesystem. And all that on server with heavy load. Without connection handling - function ignore_user_abort() - this process can be interrupted by user and final step will never be done.
18-Sep-2004 01:16
The point mentioned in the last comment isn't always the case.
If a user's connection is lost half way through an order processing script is confirming a user's credit card/adding them to a DB, etc (due to their ISP going down, network trouble... whatever) and your script tries to send back output (such as, "pre-processing order" or any other type of confirmation), then your script will abort -- and this could cause problems for your process.
I have an order script that adds data to a InnoDB database (through MySQL) and only commits the transactions upon successful completion. Without ignore_user_abort(), I have had times when a user's connection dropped during the processing phase... and their card was charged, but they weren't added to my local DB.
So, it's always safe to ignore any aborts if you are processing sensitive transactions that should go ahead, whether your user is "watching" on the other end or not.
12-Feb-2004 03:01
I don't think the first example given below will occur in the real world.
As long as your order handling script does not output anything, there's no way that it will be aborted before it completes processing (unless it timeouts). PHP only senses user aborts when a script sends output. If there's no output sent to the client before processing completes, which is presumably the case for an order handling script, the script will run to completion.
So, the only time a script can be terminated due to the user hitting stop is when it sends output. If you don't send any output until processing completes, you don't have to worry about user aborts.
07-Aug-2003 09:32
These functions are very useful for example if you need to control when a visitor in your website place an order and you need to check if he/she didn't clicked the submit button twice or cancelled the submit just after have clicked the submit button.
If your visitor click the stop button just after have submitted it, your script may stop in the middle of the process of registering the products and do not finish the list, generating inconsistency in your database.
With the ignore_user_abort() function you can make your script finish everything fine and after you can check with register_shutdown_function() and connection_aborted() if the visitor cancelled the submission or lost his/her connection. If he/she did, you can set the order as not confirmed and when the visitor came back, you can present the old order again.
To prevent a double click of the submit button, you can disable it with javascript or in your script you can set a flag for that order, which will be recorded into the database. Before accept a new submission, the script will check if the same order was not placed before and reject it. This will work fine, as the script have finished the job before.
Note that if you use ob_start("callback_function") in the begin of your script, you can specify a callback function that will act like the shutdown function when our script ends and also will let you to work on the generated page before send it to the visitor.
