How to Close a Connection Early

How do I close a connection early?

The following PHP manual page (incl. user-notes) suggests multiple instructions on how to close the TCP connection to the browser without ending the PHP script:

  • Connection handling Docs

Supposedly it requires a bit more than sending a close header.


OP then confirms: yup, this did the trick: pointing to user-note #71172 (Nov 2006) copied here:

Closing the users browser connection whilst keeping your php script running has been an issue since [PHP] 4.1, when the behaviour of register_shutdown_function() was modified so that it would not automatically close the users connection.

sts at mail dot xubion dot hu Posted the original solution:

<?php
header("Connection: close");
ob_start();
phpinfo();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
sleep(13);
error_log("do something in the background");
?>

Which works fine until you substitute phpinfo() for echo('text I want user to see'); in which case the headers are never sent!

The solution is to explicitly turn off output buffering and clear the buffer prior to sending your header information. Example:

<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // just to be safe
ob_start();
echo('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here
sleep(30);
echo('Text user will never see');
?>

Just spent 3 hours trying to figure this one out, hope it helps someone :)

Tested in:

  • IE 7.5730.11
  • Mozilla Firefox 1.81

Later on in July 2010 in a related answer Arctic Fire then linked two further user-notes that were-follow-ups to the one above:

  • Connection Handling user-note #89177 (Feb 2009)
  • Connection Handling user-note #93441 (Sep 2009)

How do I close a connection early in laravel?

I have faced the same case with you. Namely running certain scripts asyncronous to keep response times fast. Some of the methods, like the ones you mentioned, did not work as expected. I have also used Spatie\Async but the php module doesn't work when running in apache2.
So the solution, I use the exec function to run the command line on which it runs in the background. This may be a bit risky, but with care it may be one of the best practices.

exec('(/path/to/script > /dev/null) &');

PHP closing a connection early. script hangs if any output is done

The classic code to do that is:

ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional ob_start();

echo ('Text the user will see');

$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !

// Do processing here
sleep(30);

echo('Text user will never see');

Otherwise, I advise the following read if you want to do asynchronous calls: Methods for asynchronous processes in PHP

Closing a connection early with PHP

The right way to do this is to use php in fastcgi mode and then you can use the function
fastcgi_finish_request();
it will do exactly what you need - it will close the connection to the browser but the rest of the script will continue to run to the end.

http://php-fpm.org/wiki/Features

It's much better than relying on output buffering.

Closing connection to browser early and returning respond status

An http client expects response only after it has completed sending the data. You should modify the client to make multiple requests of small sizes so a response can be sent to it instead of having server close the connection (which will not work and isn't working as you've tested already).

Check the http handler implemented in http://slfileupload.codeplex.com/ for a sample of how it is typically done.

How to close HTTP connection in node.js using Express

use this

res.end();

and it will work to close connection

flask - When to close mysql connection?

One reason, not to close the cursor where you did, could be, that cur.fetchall() could return
an iterator what could cause troubles, if the iterator is not fully processed before the cursor is closed. In SQLAlchemy, this seems to be the case, since fetchall really seems to return an iterator (according to docs).

So you could do better this:

articles = list(cur.fetchall())
cur.close()

This will make sure, that the iterator is exhausted before you close the cursor.

Only in the case you deal with really big databases, you could do a little better with this (save space for the list -- but the render result will be most likely very big anyway):

articles = cur.fetchall()
cur.close()

if result > 0:
value = render_template("dashboard.html", articles = articles)
cur.close()
return value
else:
cur.close()
msg = "No Articles Yet"
return render_template("dashboard.html", msg = msg)

And back to your original question: Yes, the original code was totally wrong, because the close will never be reached, when the method is left with return before. The intention might be that what I described above, but this was just not right.



Related Topics



Leave a reply



Submit