By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

"header" call doesn't work before lenghty operation

P: n/a
a
Hi everybody,

My config: Win XP (or 2003), Apache 2.0.54, PHP 5.1.2.

I have been trying to handle the case of a lenghty opearation on the server
while providing the user with feedback and ability to cancel, and nothing
seems to work so far.

The following code is one attempt to address this, and it is called as
result of a POST:

//*************start code***************
session_start;

// prepare exec

// flush session data
session_write_close;

// trying to display a "Please wait..." message:
header( "Location: please_wait.php" );

// long exec
exec( ... );

// finally show the result of the exec
header( "Location: exec_result.php" );
//************end code*******************

What happens is that only the last header statement has a visual effect, the
first one doesn't do anything, so the user is not presented with the "Please
wait message".

I would appreciate any ideas or code examples on this or the issue of
handling this sort of lengthy operations in general.

Thanks,

A
Feb 18 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a

"a" <xx*****@pacbell.net> wrote in message
news:ss*****************@newssvr27.news.prodigy.ne t...
Hi everybody,

My config: Win XP (or 2003), Apache 2.0.54, PHP 5.1.2.

I have been trying to handle the case of a lenghty opearation on the
server while providing the user with feedback and ability to cancel, and
nothing seems to work so far.

The following code is one attempt to address this, and it is called as
result of a POST:

//*************start code***************
session_start;

// prepare exec

// flush session data
session_write_close;

// trying to display a "Please wait..." message:
header( "Location: please_wait.php" );
Location should be followed immediately by exit, at least according to the
documentation. it is supposed to transfer you to a new URL. your long exec
and other header should be in your please wait page, but the header cannot
be, because header() only works at the top of the document before any HTML
is output and before any blank lines (I wouldn't even trust whitespace) are
seen. after all your headers are output, then output a blank line to show
end of headers and then your HTML. sorry. you only get one header() per
page.


// long exec
exec( ... );

// finally show the result of the exec
header( "Location: exec_result.php" );
//************end code*******************

What happens is that only the last header statement has a visual effect,
the first one doesn't do anything, so the user is not presented with the
"Please wait message".

I would appreciate any ideas or code examples on this or the issue of
handling this sort of lengthy operations in general.

Thanks,

A

Feb 18 '06 #2

P: n/a
a
Thanks for your reply.

Location should be followed immediately by exit, at least according to the
documentation. it is supposed to transfer you to a new URL. your long
exec and other header should be in your please wait page, but the header
cannot be, because header() only works at the top of the document before
any HTML is output and before any blank lines (I wouldn't even trust
whitespace) are seen. after all your headers are output, then output a
blank line to show end of headers and then your HTML. sorry. you only get
one header() per page.


Here is a new attempt, but I still don't see the Please Wait page, instead
it goes straight to the show_result.php

//*********** the form processing.php code************

// long exec (asynchronous - it exits immediately but continues processing),
its end will tested in the next script
exec( ... );

header( "Location: please_wait.php" );

echo( "\nPlease wait" );

flush;
exit;
*********** end of form processing

//********** the "please wait.php" code *************
while( long_operation_not_done )
usleep( 100000 ); // check the operation each 0.1 secs

header( "Location: show_result.php" );

exit;
//************ end code *****************

If I put the echo in second php script, I get an error about headers already
sent or similiar.

I don't know php well enough to figure out a solution to unblock me at this
point, so any new idea would be highly appreciated.

A
Feb 18 '06 #3

P: n/a
a wrote:
Here is a new attempt, but I still don't see the Please Wait page,
instead it goes straight to the show_result.php

You could try something as follows instead:

<?php

if (isset($_GET['pause'])) {
sleep(10);
print 'Done';
exit;
}

print 'Please wait';
header("Refresh: 1;url={$_SERVER['PHP_SELF']}?pause=1");

?>
flush;
exit;


flush();
exit;

(flush() is a function and should be called with parenthesis, while exit is
a language construct where the parenthesis indeed can be omitted).
JW
Feb 18 '06 #4

P: n/a
Janwillem Borleffs wrote:
a wrote:
Here is a new attempt, but I still don't see the Please Wait page,
instead it goes straight to the show_result.php

You could try something as follows instead:

<?php

if (isset($_GET['pause'])) {
sleep(10);
print 'Done';
exit;
}

print 'Please wait';
header("Refresh: 1;url={$_SERVER['PHP_SELF']}?pause=1");

?>
flush;
exit;

flush();
exit;

(flush() is a function and should be called with parenthesis, while exit is
a language construct where the parenthesis indeed can be omitted).
JW


Won't work. You can't send a header call after ANY output.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #5

P: n/a
a wrote:
Thanks for your reply.
Location should be followed immediately by exit, at least according to the
documentation. it is supposed to transfer you to a new URL. your long
exec and other header should be in your please wait page, but the header
cannot be, because header() only works at the top of the document before
any HTML is output and before any blank lines (I wouldn't even trust
whitespace) are seen. after all your headers are output, then output a
blank line to show end of headers and then your HTML. sorry. you only get
one header() per page.

Here is a new attempt, but I still don't see the Please Wait page, instead
it goes straight to the show_result.php

//*********** the form processing.php code************

// long exec (asynchronous - it exits immediately but continues processing),
its end will tested in the next script
exec( ... );

header( "Location: please_wait.php" );

echo( "\nPlease wait" );

flush;
exit;
*********** end of form processing

//********** the "please wait.php" code *************
while( long_operation_not_done )
usleep( 100000 ); // check the operation each 0.1 secs

header( "Location: show_result.php" );

exit;
//************ end code *****************

If I put the echo in second php script, I get an error about headers already
sent or similiar.

I don't know php well enough to figure out a solution to unblock me at this
point, so any new idea would be highly appreciated.

A


I don't think that's going to be very successful, either. You can't
predict what the browser will do as for caching, for instance.

Maybe flash would work?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #6

P: n/a
Jerry Stuckle wrote:
Won't work. You can't send a header call after ANY output.


Dude, don't judge before trying.
JW
Feb 18 '06 #7

P: n/a
Janwillem Borleffs wrote:
Jerry Stuckle wrote:
Won't work. You can't send a header call after ANY output.

Dude, don't judge before trying.
JW


First of all, I am not your "Dude". And I don't appreciate your
patronizing attitude.

I suggest you learn a few manners. I've probably been doing PHP longer
than you have - and been programming longer than you've been alive.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #8

P: n/a
Jerry Stuckle wrote:
I suggest you learn a few manners. I've probably been doing PHP
longer than you have - and been programming longer than you've been
alive.


You are making assumptions again...
JW
Feb 18 '06 #9

P: n/a
Janwillem Borleffs wrote:
Jerry Stuckle wrote:
I suggest you learn a few manners. I've probably been doing PHP
longer than you have - and been programming longer than you've been
alive.

You are making assumptions again...
JW


I very much doubt it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #10

P: n/a
On Sat, 18 Feb 2006 16:21:57 +0100, "Janwillem Borleffs" <jw@jwscripts.com>
wrote:
Jerry Stuckle wrote:
Won't work. You can't send a header call after ANY output.


Dude, don't judge before trying.


You cannot send an HTTP header after any part of the body has been sent.

In some circumstances you can _call_ header() after _calling_ functions that
generate output, i.e. when you have output buffering on, because the header and
the content go into separate buffers, and PHP sends them out in the right order
when it comes to flushing the buffers and actually starting to send to the
client.

But once content has started to go to the client (i.e. immediately in the
default configuration, or after the first flush in buffered configurations),
you can't send an HTTP header, since there's no way in HTTP to send headers in
the middle of the message body.

Your code from the prior post produces:

Please wait
Warning: Cannot modify header information - headers already sent by (output
started at /home/andyh/public_html/test.php:9) in
/home/andyh/public_html/test.php on line 10

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Feb 18 '06 #11

P: n/a
a
Thanks all for your replies.

I'm glad I sparked a (somewhat heated) debate on the subject, but I still
don't see a solution with predictable results to the issue of asynchronous
processing on the server while displaying feedback to the user, at least not
if the requirement is for it to be implemented entirely on the server using
php. But this is most likely due to my lack of knowledge - there must be a
bunch of apps that do lengthy operations on the server and which provide
user feedback, a cancel option etc.

Another solution would be to use some javascript on the client. My concern
is that not all browsers implement java script, and even if present, some
users may disable it.

So does it make sense to implement a solution based on js and require users
to use a js capable browser and to have it enabled? Are there any stats as
to what % of users would be lost this way?

Thanks

A
Feb 18 '06 #12

P: n/a
Andy Hassall wrote:
Please wait
Warning: Cannot modify header information - headers already sent by
(output started at /home/andyh/public_html/test.php:9) in
/home/andyh/public_html/test.php on line 10


Weird thing is that while I have error_reporting set to E_ALL, the error
didn't show up on my system. Must have broken something during the last
upgrade...
JW
Feb 18 '06 #13

P: n/a
you have to do something like this:
(is an example to form procesing)

----form.php------
start sessions
show form
form action=pleasewait.php

---pleasewait.php---
start sessions
do exec.
show "please wait" page but insert a meta-refresh tag like this:
<meta http-equiv="refresh" content="3;url=./result.php" />
in the header.
It will redirect automatically to the next script

---result.php-----
show results page.

Feb 18 '06 #14

P: n/a
a
Thanks for your reply.
you have to do something like this:
(is an example to form procesing)

----form.php------
start sessions
show form
form action=pleasewait.php

---pleasewait.php---
start sessions
do exec.
show "please wait" page but insert a meta-refresh tag like this:
<meta http-equiv="refresh" content="3;url=./result.php" />
in the header.
It will redirect automatically to the next script

---result.php-----
show results page.


I'm still not sure this is going to work, because after exec which returns
immediately, I still have to check that the long operation has finished and
then show the result, and that can take any amount of time.

The process that is exec'ed signals the end of the processing by creating a
file, and in my script I something like this:

while( !file_exists( $signal_end_file ) )
sleep( 1 );

While this loop is going on, I can't do anything else in the scripts
associated with this session.

Is there any other way to check for the end of the operation on the server,
that would not block, for example event based etc.

Thanks,

A
Feb 19 '06 #15

P: n/a
On 2006-02-18, a <xx*****@pacbell.net> wrote:
Thanks all for your replies.

I'm glad I sparked a (somewhat heated) debate on the subject, but I still
don't see a solution with predictable results to the issue of asynchronous
processing on the server while displaying feedback to the user, at least not
if the requirement is for it to be implemented entirely on the server using
php. But this is most likely due to my lack of knowledge - there must be a
bunch of apps that do lengthy operations on the server and which provide
user feedback, a cancel option etc.

Another solution would be to use some javascript on the client. My concern
is that not all browsers implement java script, and even if present, some
users may disable it.

So does it make sense to implement a solution based on js and require users
to use a js capable browser and to have it enabled? Are there any stats as
to what % of users would be lost this way?

Thanks

A


If you know how long the process will take send a redirect header with the
apropriate timeout, then send content. flushing as neccessary.

1% complete
2% complete
3% complete etc...

True asynchronous feedback requires separating the "background process" from
the web server process and providing a way for them to communicate (eg
writing status info to a file)

Bye.
Jasen
Feb 19 '06 #16

P: n/a
a
>
If you know how long the process will take send a redirect header with the
apropriate timeout, then send content. flushing as neccessary.

1% complete
2% complete
3% complete etc...
The duration of the background process is not known, it can take seconds,
minutes, or even longer. It all depends on the parameters that the user has
submitted.

True asynchronous feedback requires separating the "background process"
from
the web server process and providing a way for them to communicate (eg
writing status info to a file)


This the way things work now, but I couldn't find a way to make the php
scripts perform the following tasks concurrently:
1. monitor the status of the background process
2. give feedback to the user
3. react to other user commands such as cancel or back in the browser

Thanks,

A
Feb 19 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.