php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #24797 feature request: way to close connection with client?
Submitted: 2003-07-24 15:30 UTC Modified: 2003-07-26 19:24 UTC
From: spagmoid at yahoo dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.3.2 OS: WinXP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: spagmoid at yahoo dot com
New email:
PHP Version: OS:

 

 [2003-07-24 15:30 UTC] spagmoid at yahoo dot com
Description:
------------
feature request: A way to close the connection with the client, while keeping the script running?  Is this possible?  

Also helpful to do a flush that will result in final sending of data when using mod_gzip etc.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-25 04:18 UTC] magnus@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Use register_shutdown_function()
 [2003-07-25 10:26 UTC] spagmoid at yahoo dot com
Why are you telling me this is not "a bug in PHP itself"?

I didn't submit a bug, I submitted a feature change/request.  That's why it says "FEATURE REQUEST" in about 20 places.

register_shutdown_function() does not do what I described.  I want the script to stay running AFTER the connection is closed.  Data can still be sent in the function called by register_shutdown_function(), therefore the connection must still be OPEN.
 [2003-07-26 12:32 UTC] pollita@php.net
The message magnus@php.net included with his bogusifying was a template response.  For the purposes of this bug reporting system Feature Requests and Bugs are largely equivalent.   So.... scale back the attitude m'kay?

Second, this *IS* bogus.  As a quick look at the manual will show a chapter on "Connection Handling" in which you'll find a clearly documented reference to: http://www.php.net/manual/en/function.ignore-user-abort.php which will accomplish what you describe in the first half of your report.

What you describe in the second half of your report directly contradicts the first half.  You want the connection closed but you want to send the last of the mod_gzip data?


 [2003-07-26 16:22 UTC] spagmoid at yahoo dot com
Jesus you people are dense.  You lose the attitude.
Then me try to explain this in a way you can understand:

1. script runs
2. script sends data
3. script ends connection, sending all data <- feature missing
4. script does other stuff
5. script exits
 [2003-07-26 18:35 UTC] pollita@php.net
Again, through what mechanism do you intend to send data to the client *after* the connection is closed?

But to address your original question.  There is no way PHP can possibly close the data stream between itself and the client since this stream is handled by the webserver.

The only signal PHP can send to the webserver is "I'm done executing."  It sends this "signal" by terminating.  At this point your script can not continue executing because, obviously, it has stopped.

The only time PHP can continue executing after the data stream has closed is when the browser closes the connection itself.

Now, that said, might I suggest you attempt to ilicit a more courteous response in the future by explaining your question in detail and making an attempt at civility by not attacking those who take the time to respond.

 [2003-07-26 18:42 UTC] spagmoid at yahoo dot com
I DO NOT WANT TO SEND DATA TO THE CLIENT AFTER THE CONNECTION IS CLOSED.  NOWHERE DID I SAY THAT.  That is what you people keep trying to tell me to do.  I don't want to do that.  I never wanted to do that.  I will never want to do that.  That is ridiculous.

"The only time PHP can continue executing after the data stream has closed is when the browser closes the connection itself."

I would be more inclined to believe this if you had understood any of the question.

"The only signal PHP can send to the webserver is "I'm done executing." It sends this "signal" by terminating."

There may be another way to send this signal, but I doubt if either of you are in a position to tell me.

"explaining your question in detail and making an attempt at civility by not attacking those who take the time to respond."

It was in detail.  "A way to close the connection with the client, while keeping the script running?"  That's it, period.  It's not a complicated issue.

Sending a form letter that doesn't even match the question is not "taking the time to respond".
 [2003-07-26 19:24 UTC] rasmus@php.net
Sara already answered your request by pointing you at the connection handling chapter in the manual.  You are asking for a way to keep executing your script after the connection has been closed.  Closing the connection itself from PHP is of course impossible, but if the remote client decides to close the connection then you can allow PHP to continue executing.

Please stop re-opening this.  
 [2004-03-25 14:18 UTC] php at digdug dot cx
I am looking for the same feature.

Here is the scenario: A user submits a form into script.php. The script needs to do two things:

1) Tell the user that the submission has been accepted. (It always is.)
2) Do some background processing on the submission and flag it if necessary. This takes up to half a minute, so I don't want to make the user wait for it to finish.

So here is what I am trying to do:
<? insert_submission_into_database($submission); ?>
<html><body>
<? echo("submission accepted"); ?>
</html></body>
<?
   close_connection_to_browser(); // currently not possible
   if (submission_bad()) flag_submission();
   // no further info is sent to the browser
?>
 [2004-06-07 15:18 UTC] john_ramsden at sagitta-ps dot com
> Closing the connection itself from PHP is of course
> impossible, but if the remote client decides to close
> the connection then you can allow PHP to continue
> executing.

I have the same requirement as Spagmoid's, albeit for a different reason:

In my case it's a PHP script called by an SQL trigger - The script has to read the updated records (to replicate them in another database); but the updated records aren't available until the trigger completes, and the trigger can't complete until the PHP script disconnects! DUH!

Like Spagmoid's, my script obviously wouldn't try to send data after the disconnect, as this doesn't make sense.

I must say, the PHP developers seem a bit dogmatic about this issue. Why should closing the connection from PHP be "of course impossible"? What is to stop PHP having some interface to tell Apache to do that without terminating the script itself?

I realize there's a simple workaround: the script can spawn an autonomous background process to do "post processing". But I'm not aware of a portable PHP call to do this; so for a platform-independent app, it would be lot cleaner if "early disconnects", followed by post-processing, could be done in a single PHP script.
 [2011-10-22 11:49 UTC] lucb1e at hotmail dot com
I would like to see this feature too, currently you'd need a very cumbersome way to do this (as far as I know at least). This example is about an uploading script, which also synchronises with other servers immediately (so no cronjob which would otherwise do the job).

The user opens the uploading page, and gets an upload form together with an invisible iframe. This iframe is a php script that opens a listening socket and saves in a database table which port it is on (together with the client's IP or, if available, session ID). This iframe-script also sets ignore_user_abort.
When the user uploads a file, the processing script will do the following:
- Put the file somewhere and generate a download link;
- Echo the downloadlink to the user;
- ob_flush in the hope that the browser is smart enough to display the page already;
- Lookup the listening socket for this client in the database;
- Put the filename through to the iframe-script-which-has-listening-socket;
- die().

The 'iframe-script' then knows where the file is located which was just uploaded, and without the user waiting for it, it can synchronise it with other servers - or do whatever it would like to do.


This technique would theoratically work allright, but has a number of problems:
- Many webhosts do not support sockets at all;
- Listening sockets have even worse support as normal (client) sockets;
- Not all hosts allow you to set a custom maximum_execution_time, which is needed to prevent the iframe-script from killing itself;
- I don't expect all hosts to support ignore_user_abort, though I didn't look into that yet;
- It is unnessesarily complex compared to how easy it could be done with a kill_client_connection() function.
 [2013-07-30 09:04 UTC] sushantahirrao at gmail dot com
Hello,
I am facing the same issue.
Following is the scenario,
1. User opens edit profile page
2. User puts an image file for uploading
3. User cancels request before any response from the server
4. Image file keeps being uploaded in the background even if the connection is 
closed


Expected Result:
Image file should not be uploaded if client closes the connection


Actual Result:
User is still able to see newly uploaded pic even if he had closed connection 
with the server.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 23:01:34 2024 UTC