RE: [PHP] Closing a connection to browser without exiting the script

This is a discussion on RE: [PHP] Closing a connection to browser without exiting the script within the PHP General forums, part of the PHP Programming Forums category; > -------- Original Message -------- > Subject: [php] Closing a connection to browser without exiting the > script > From: David Né...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-02-2006
ray.hauge@americanstudentloan.com
 
Posts: n/a
Default RE: [PHP] Closing a connection to browser without exiting the script

> -------- Original Message --------
> Subject: [php] Closing a connection to browser without exiting the
> script
> From: David Négrier <d.negrier@thecodingmachine.com>
> Date: Wed, November 01, 2006 2:24 pm
> To: php-general@lists.php.net
>
> Hello there,
>
> I'm having a somewhat unusual question here, and I cannot find any way
> to solve it.
>
> I have a PHP page that displays a message, and then, performs a very
> long operation. Note that it displays the message first.
> I do not intend to give some feedback to the user when the operation is
> done.
>
> I've seen I can use ignore_user_abort() to prevent the user from
> stopping the ongoing operation, but that solves only part of my problem.
> Because as long as the page is not fully loaded, the mouse cursor in the
> user's browser is showing a watch.
>
> So ideally, what I would like is to be able to close the connection from
> the server-side, but without using the exit() function, so my script
> keeps running afterwards.
>
> I know I could use a system() call to launch another process to do the
> processing, but I would like to avoid doing that, because there are many
> variables in the context that I cannot easily pass in parameter.
>
> I also tried to use the register_shutdown_function() to perform my
> operation after the page is displayed but since PHP 4.1.0, the
> connection is closed after the function is called....
>
> Would any of you have an idea on how I could close that connection?
>
> Thanks a lot,
> David
> www.thecodingmachine.com


It's a bit different of an idea, but you could put your reqest to
perform the operation in a text file in some directory. Then you have
a seperate PHP script that checks that directory for any files that
would tell it to operate. Then you tell Cron to run that script every
minute, 10 minutes, whatever works best for you.

With this idea you can write out the file and then tell the person that
you have submitted a request. Then the cron job checks for the request
and performs it. You could use a database instead of a file as well
with the idea of a queue. That would probably work better, because
then if you needed to you can keep track of which user's process was
run, and then update the "user" table. Say the "user" table has a
column "processing" with three possible values. 0 (not processed), 1
(in processing), 2 (finished processing). You could probably expand
upon that idea quite a bit more if you need to.

It's no single magic bullet function call, but that's how I'd probably
do it.

Ray
Reply With Quote
  #2 (permalink)  
Old 11-03-2006
Ed Lazor
 
Posts: n/a
Default Re: [PHP] Closing a connection to browser without exiting the script

Here's another idea:

display your message in the original browser window, launch a new
browser window for the processing script, have the window set behind
the first with javascript. When your script is finished, have it
output javascript that closes the "processing" window.


On Nov 2, 2006, at 12:37 PM, ray.hauge@americanstudentloan.com wrote:

>> -------- Original Message --------
>> Subject: [php] Closing a connection to browser without exiting the
>> script
>> From: David Négrier <d.negrier@thecodingmachine.com>
>> Date: Wed, November 01, 2006 2:24 pm
>> To: php-general@lists.php.net
>>
>> Hello there,
>>
>> I'm having a somewhat unusual question here, and I cannot find any
>> way
>> to solve it.
>>
>> I have a PHP page that displays a message, and then, performs a very
>> long operation. Note that it displays the message first.
>> I do not intend to give some feedback to the user when the
>> operation is
>> done.
>>
>> I've seen I can use ignore_user_abort() to prevent the user from
>> stopping the ongoing operation, but that solves only part of my
>> problem.
>> Because as long as the page is not fully loaded, the mouse cursor
>> in the
>> user's browser is showing a watch.
>>
>> So ideally, what I would like is to be able to close the
>> connection from
>> the server-side, but without using the exit() function, so my script
>> keeps running afterwards.
>>
>> I know I could use a system() call to launch another process to do
>> the
>> processing, but I would like to avoid doing that, because there
>> are many
>> variables in the context that I cannot easily pass in parameter.
>>
>> I also tried to use the register_shutdown_function() to perform my
>> operation after the page is displayed but since PHP 4.1.0, the
>> connection is closed after the function is called....
>>
>> Would any of you have an idea on how I could close that connection?
>>
>> Thanks a lot,
>> David
>> www.thecodingmachine.com

>
> It's a bit different of an idea, but you could put your reqest to
> perform the operation in a text file in some directory. Then you have
> a seperate PHP script that checks that directory for any files that
> would tell it to operate. Then you tell Cron to run that script every
> minute, 10 minutes, whatever works best for you.
>
> With this idea you can write out the file and then tell the person
> that
> you have submitted a request. Then the cron job checks for the
> request
> and performs it. You could use a database instead of a file as well
> with the idea of a queue. That would probably work better, because
> then if you needed to you can keep track of which user's process was
> run, and then update the "user" table. Say the "user" table has a
> column "processing" with three possible values. 0 (not processed), 1
> (in processing), 2 (finished processing). You could probably expand
> upon that idea quite a bit more if you need to.
>
> It's no single magic bullet function call, but that's how I'd probably
> do it.
>
> Ray
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Reply With Quote
  #3 (permalink)  
Old 11-03-2006
John Comerford
 
Posts: n/a
Default Re: [PHP] Closing a connection to browser without exiting the script

You could also use an Ajax call from the main window to start the
processing without opening a second window.

HTH,
John

Ed Lazor wrote:
> Here's another idea:
>
> display your message in the original browser window, launch a new
> browser window for the processing script, have the window set behind
> the first with javascript. When your script is finished, have it
> output javascript that closes the "processing" window.
>
>
> On Nov 2, 2006, at 12:37 PM, ray.hauge@americanstudentloan.com wrote:
>
>>> -------- Original Message --------
>>> Subject: [php] Closing a connection to browser without exiting the
>>> script
>>> From: David Négrier <d.negrier@thecodingmachine.com>
>>> Date: Wed, November 01, 2006 2:24 pm
>>> To: php-general@lists.php.net
>>>
>>> Hello there,
>>>
>>> I'm having a somewhat unusual question here, and I cannot find any way
>>> to solve it.
>>>
>>> I have a PHP page that displays a message, and then, performs a very
>>> long operation. Note that it displays the message first.
>>> I do not intend to give some feedback to the user when the operation is
>>> done.
>>>
>>> I've seen I can use ignore_user_abort() to prevent the user from
>>> stopping the ongoing operation, but that solves only part of my
>>> problem.
>>> Because as long as the page is not fully loaded, the mouse cursor in
>>> the
>>> user's browser is showing a watch.
>>>
>>> So ideally, what I would like is to be able to close the connection
>>> from
>>> the server-side, but without using the exit() function, so my script
>>> keeps running afterwards.
>>>
>>> I know I could use a system() call to launch another process to do the
>>> processing, but I would like to avoid doing that, because there are
>>> many
>>> variables in the context that I cannot easily pass in parameter.
>>>
>>> I also tried to use the register_shutdown_function() to perform my
>>> operation after the page is displayed but since PHP 4.1.0, the
>>> connection is closed after the function is called....
>>>
>>> Would any of you have an idea on how I could close that connection?
>>>
>>> Thanks a lot,
>>> David
>>> www.thecodingmachine.com

>>
>> It's a bit different of an idea, but you could put your reqest to
>> perform the operation in a text file in some directory. Then you have
>> a seperate PHP script that checks that directory for any files that
>> would tell it to operate. Then you tell Cron to run that script every
>> minute, 10 minutes, whatever works best for you.
>>
>> With this idea you can write out the file and then tell the person that
>> you have submitted a request. Then the cron job checks for the request
>> and performs it. You could use a database instead of a file as well
>> with the idea of a queue. That would probably work better, because
>> then if you needed to you can keep track of which user's process was
>> run, and then update the "user" table. Say the "user" table has a
>> column "processing" with three possible values. 0 (not processed), 1
>> (in processing), 2 (finished processing). You could probably expand
>> upon that idea quite a bit more if you need to.
>>
>> It's no single magic bullet function call, but that's how I'd probably
>> do it.
>>
>> Ray
>>
>> --PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>

>
> --PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Reply With Quote
  #4 (permalink)  
Old 11-03-2006
Myron Turner
 
Posts: n/a
Default Re: [PHP] Closing a connection to browser without exiting the script

I think the Ajax solution that John suggests is the ticket.

I did something like this for a site that books reservations for various
services. But before the booking can occur, the customer has to get info
on the various service options, which are returned from a separate
database server. This takes time and what the company needed in the
meantime was a message saying that the query was being processed, please
wait. My solution was to print the please wait message to the screen,
while accessing the database using an Ajax call. Since with an Ajax
call you are not waiting for a page to load there's no hour glass or
clock, etc., and from the user's perspective there's a seamless experience.

I used Perl for my server-side script, but if you prefer PHP you can
create a CGI script using PHP and run the same process as your original
PHP page. Just put #!/usr/bin/php at the top of the script, before the
<?php instruction.


Myron

John Comerford wrote:
> You could also use an Ajax call from the main window to start the
> processing without opening a second window.
>
> HTH,
> John
>
> Ed Lazor wrote:
>> Here's another idea:
>>

>
>>>>
>>>> I have a PHP page that displays a message, and then, performs a very
>>>> long operation. Note that it displays the message first.
>>>> I do not intend to give some feedback to the user when the operation is
>>>> done.
>>>>
>>>> I've seen I can use ignore_user_abort() to prevent the user from
>>>> stopping the ongoing operation, but that solves only part of my
>>>> problem.
>>>> Because as long as the page is not fully loaded, the mouse cursor in
>>>> the
>>>> user's browser is showing a watch.
>>>>
>>>> So ideally, what I would like is to be able to close the connection
>>>> from
>>>> the server-side, but without using the exit() function, so my script
>>>> keeps running afterwards.
>>>>

>>



--

_____________________
Myron Turner
http://www.mturner.org/XML_PullParser/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:01 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0