473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

"header" call doesn't work before lenghty operation

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
16 2888

"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kornelius Finkenbein | last post by:
Hello folks! I've got a strange problem with my download-script in conjunction with M$ internet explorer, if the filename I want to link to includes more than one points. In Netscape the problem...
1
by: Danny Anderson | last post by:
Hola, PHP folk! I have a php page that contains a self-processing form. The form holds search results. The search terms originally came from the previous page, but the user can repeatedly...
7
by: beliavsky | last post by:
Ideally, one can use someone's C++ code by just looking at the header files (which should contain comments describing the functions in addition to function definitions), without access to the full...
4
by: John Smith | last post by:
I have a project that consists of about a dozen translation units. I use a command line compiler and it occured to me that I could simplify compiling the project by #include(ing) them in a header...
23
by: lwoods | last post by:
I am trying to pass some info to another page on my site. I set "session_start()" in page 1, assign a session variable to a value, then execute a "header('Location: ....')." But on the target...
21
by: cman | last post by:
does anyone know why i can't generate images with: header("Content-type:image/jpeg"); imagejpeg($img_number); i've tried different examples but i always get a text output as if the header...
28
by: fred.haab | last post by:
Not having server side scripting, I've been doing this for "last modified" tags on my pages: <div class="modified"> <script type="Text/JavaScript"> <!-- document.write("This page was last...
17
by: StevePBurgess | last post by:
A really simple script is driving me up the wall. I have a very simply facility on a website to allow the user to reorder items in a database table as she wishes by clicking a link that (in this...
45
by: Umesh | last post by:
please help. thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.