EDIT
This is what the code is for:
- The user clicks a button which starts a long-running process on the server (not actually uploading a file, that's just to get the code working)
- If the process finishes before sending the response nothing seems to be happening on the user's browser and they might click again/reload the page/do something else undesirable
- An immediate response is required to let the user know they have to wait
- A progress indicator shows how far the process has got, i.e. roughly how long the user will have to wait
I just need some way of telling the user in real-time roughly what the progress on the server is. After they click, everything happens on the server until the process in finished, then they get a redirection signal and go to the next page.
If I need to throw away all my code and try something else I'll do it!
END EDIT
The question has been asked many times but I can't find an answer that actually works yet.
I'm setting up a progress indicator for the first time and I have it mostly working except for a lot of warnings. Here is the code:
<?php
session_name('test');
$filesize = 10000000;
// Allow script to send response but keep running afterwards
ignore_user_abort(true);
ob_start();
header('Connection: close');
header('Content-Length: ' . ob_get_length());
ob_end_flush();
//ob_flush();
flush();
for ($i = 0; $i < $filesize; $i += rand(4096, 8192)) { // Just testing...
ob_start();
@session_start();
$_SESSION['progress'] = $i;
session_write_close();
ob_end_clean();
}
ob_start();
@session_start();
$_SESSION['progress'] = $filesize;
session_write_close();
ob_end_clean();
exit;
?>
I have a second script which then reads the values out of $_SESSION
which is workig fine.
If I remove the @
s before session_start();
in the above script I get multitudes of the following error:
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP Warning: session_start(): Cannot send session cookie - headers already sent in C:\\server\\Apache24\\htdocs\\localhost\\SRI\\progress.php on line 36, referer: http://localhost/SRI/progress.php
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP Stack trace:, referer: http://localhost/SRI/progress.php
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP 1. {main}() C:\\server\\Apache24\\htdocs\\localhost\\SRI\\progress.php:0, referer: http://localhost/SRI/progress.php
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP 2. session_start() C:\\server\\Apache24\\htdocs\\localhost\\SRI\\progress.php:36, referer: http://localhost/SRI/progress.php
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent in C:\\server\\Apache24\\htdocs\\localhost\\SRI\\progress.php on line 36, referer: http://localhost/SRI/progress.php
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP Stack trace:, referer: http://localhost/SRI/progress.php
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP 1. {main}() C:\\server\\Apache24\\htdocs\\localhost\\SRI\\progress.php:0, referer: http://localhost/SRI/progress.php
[Fri Aug 22 08:59:59.109375 2014] [:error] [pid 4100:tid 1248] [client 127.0.0.1:2577] PHP 2. session_start() C:\\server\\Apache24\\htdocs\\localhost\\SRI\\progress.php:36, referer: http://localhost/SRI/progress.php
I really don't like using @
as it only suppresses the warnings; I'd much rather fix them. Is there a cleaner way of getting this working?
I have already eliminated the following:
- UTF-8 BOM
- Output between
flush()
and the nextsession_start()
- Using output buffering (ob) for every
session_start()
- Using
pcntl_fork()
(doesn't work under Apache)
The second lot of ob_start()
s have helped with getting a timely response to the browser but my error log is still filling up! I suspect that since the flush()
has sent headers and I'm still in the same script that this is why the warnings keep happening. I have a WAMP server for development but will be transferring the working program to a LAMP server.