php - close a connection early -


i'm attempting ajax call (via jquery) initiate long process. i'd script send response indicating process has started, jquery won't return response until php script done running.

i've tried "close" header (below), , output buffering; neither seems work. guesses? or need in jquery?

<?php  echo( "we'll email done." );  header( "connection: close" );  // stuff take while  mail( 'dude@thatplace.com', "okay i'm done", 'yup, done.' );  ?> 

the following php manual page (incl. user-notes) suggests multiple instructions on how close tcp connection browser without ending php script:

supposedly requires bit more sending close header.


op confirms: yup, did trick: pointing user-note #71172 (nov 2006) copied here:

closing users browser connection whilst keeping php script running has been issue since [php] 4.1, when behaviour of register_shutdown_function() modified not automatically close users connection.

sts @ mail dot xubion dot hu posted 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 in background"); ?> 

which works fine until substitute phpinfo() echo('text want user see'); in case headers never sent!

the solution explicitly turn off output buffering , clear buffer prior sending header information. example:

<?php ob_end_clean(); header("connection: close"); ignore_user_abort(true); // safe ob_start(); echo('text user see'); $size = ob_get_length(); header("content-length: $size"); ob_end_flush(); // strange behaviour, not work flush(); // unless both called ! // processing here  sleep(30); echo('text user never see'); ?> 

just spent 3 hours trying figure 1 out, hope helps :)

tested in:

  • ie 7.5730.11
  • mozilla firefox 1.81

later on in july 2010 in related answer arctic fire linked 2 further user-notes were-follow-ups 1 above:


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -