G
G
grefon2014-07-24 04:17:15
PHP
grefon, 2014-07-24 04:17:15

What could be the reason for incomplete loading of a page with a loop?

Hello dear experts!
I ran into a very peculiar problem and I can’t understand why the following situation occurs:
There is a web service for clients that is hosted on its own server:
Debian 6 Squeeze 64 bit ,
apache2-mpm-itk-2.2.16-6+squeeze12 ,
php5 -5.3.3-7+squeeze19 .
On the main page of the web service in php, certain actions are performed (the foreach loop in which the curl is called) when the form is submitted. Each time a different amount of data is sent via post, but in total there are no more than 20 iterations in the loop.
The code looks like this:

<div>Блок до цикла</div>
<?php
foreach ($_POST['account'] as $account_id) {
    $str = getPage($account_id); // ВЫЗОВ CURL ЧЕРЕЗ ФУНКЦИЮ
}
?>
<div>Блок после цикла</div>

The problem is that every 3-4 times out of 10 the page doesn't load completely and only "Block before cycle" is displayed. In this case, the browser (any) shows that the page is loaded.
Using the “scientific poke” method, I found that if at least something is inserted into the loop for output, then the page starts loading completely:
<div>Блок до цикла</div>
<?php
foreach ($_POST['account'] as $account_id) {
    echo "\r\n"; // ПРИ ДОБАВЛЕНИИ ЭТОЙ СТРОКИ БЛОК ПОСЛЕ ЦИКЛА ОТОБРАЖАЕТСЯ
    $str = getPage($account_id); // ВЫЗОВ CURL ЧЕРЕЗ ФУНКЦИЮ
}
?>
<div>Блок после цикла</div>

Inside the getPage function, curl is called, the page is parsed, and logging is done. In both the first and second cases, the cycle goes through completely, records about all iterations go to the log (txt file) .
Displaying errors in the script is enabled, there are no "bugged" functions (@getPage) , the time to execute the script is sufficient (1800 sec) , there is also enough memory allocated (1000 mb) . There are definitely enough server resources to execute the script. The problem appeared suddenly a couple of days ago, before that everything worked. The server configuration has not changed. Reboot doesn't help.
Tell me, please, why is the page not completely displayed in the browser without throwing any text into the buffer? I myself see this for the first time, while I solved the problem by outputting a new line in the loop, but I want to understand the nature of such a bug.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Zheltyakov, 2014-07-24
@VitaZheltyakov

Try replacing "professional insert" with normal output using echo.

I
ilnile, 2014-07-24
@ilnile

<?php ob_start();  //Стуртуем буферизацию 
$i=0; 
while($i<10000){ 
    echo $i.'<br>';  //Выводим счетчик 
    ob_flush();        //Отсылаем буфер в браузер 
    flush(); 
    sleep(1); 
    $i++; 
} // while 
ob_end_flush(); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question