mysql - Stop PHP script from executing if client disconnects -


i'm having following problem.

client program sends https action server. server gets action , params required. client disconnects while php script called working.

the script echos response , client doesn't shouldn't. because important action , client should response want stop php script executing if scenario ever happens.

the entire script in mysql transaction if dies db rollback occur , it's essential does.

i have put code @ end of script

ob_end_clean();  header("connection: close");  ignore_user_abort();  ob_start();  echo $response->asxml();  $size = ob_get_length();  header("content-length: $size");  ob_end_flush();  flush();  if ( connection_aborted() )         log('connection aborted'); else         log('connection fine'); 

but connection_aborted returns 0 (normal connection) script executes. connection_status doesn't help, don't know else try.

edit:

i figured out never works when script called ajax request. when called regular http shows connection dead. when call new xmlhttprequest() never figures out connection closed.

it asynchronous request.

so managed narrow down. still don't know how fix it.

after experimentation, seems need multiple flushes (writes) connection have php detect connection has aborted. seems want write output 1 big chunk of xml data. can around problem of having no more data send using chunked transfer encoding http requests.

ob_implicit_flush(true); ignore_user_abort(true);  while (ob_get_level()) {     ob_end_clean(); }  header('content-type: text/xml; charset=utf-8'); header('transfer-encoding: chunked'); header('connection: close');  // ----- mysql processing here. -----  // sleep there's time abort connection. wouldn't // here in production code, here simulate time process request. sleep(3);  // put xml content $xml. if output buffering used, dump contents $xml. //$xml = $response->asxml(); $xml = '<?xml version="1.0"?><test/>'; $size = strlen($xml);  // if connection aborted before here, connection_status() not return 0.  echo dechex($size), "\r\n", $xml, "\r\n"; // write content chunk.  echo "0\r\n\r\n"; // write closing chunk, detecting if connection closed.  if (0 !== connection_status()) {     error_log('connection aborted.');     // rollback mysql transaction. } else {     error_log('connection successful.');     // commit mysql transaction. } 

ob_implicit_flush(true) tells php make flush after each echo statement. connection status checked after each chunk sent echo. if connection closed before first statement, connection_status() should return non-zero value.

you may need use ini_set('zlib.output_compression', 0) @ top of script turn off output compression if compression turned on in php.ini, otherwise acts external buffer , connection_status() may return 0.


edit: transfer-encoding: chunked header modifies way browser expects receive data in http response. browser expects receive data in chunks form [{hexadecimal number of bytes in chunk}\r\n{data}\r\n]. example, can send string "this example string." chunk echoing 1a\r\nthis example string.\r\n. browser display string, not 1a, , keep connection open waiting until receives empty chunk, is, 0\r\n\r\n.


edit: modified code in answer works standalone script, rather rely on $response being defined op's code. is, commented line $xml = $response->asxml(); , replaced $xml = '<?xml version="1.0"?><test/>'; proof of concept.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -