AJAX: Detect connection closed, PHP continues

Asked
Active3 hr before
Viewed126 times

5 Answers

connectiondetect
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Connect and share knowledge within a single location that is structured and easy to search.,Thanks to that, thumbs can be also rendered later, when they are first time requested (even without ajax). If you don't want requesting and waiting for thumbs, use cron job on server, which will create thumbs for awaiting images.,Question: How do I get AJAX to allow the user to continue once it has received the code from the middle of the PHP script, whilst the PHP script continues to generate the thumbnails?

Example PHP code:

< ? php
// Save images to db, etc

// Now tell AJAX to let the user continue, before generating thumbnails
if (ini_get('zlib.output_compression')) {
   ini_set('zlib.output_compression', 'Off'); // turn IIS gzip for this file
}
ob_end_clean();
header("Connection: close");
header("Content-Encoding: none"); //ensures gzip is not sent through
ob_start();
echo '123'; // 3 digit number will be sent back to AJAX
$size = ob_get_length(); // should mean Content-Length = 3
header("Content-Length: $size");
ob_end_flush();
flush();
ob_end_clean();

// Generate thumbnails, etc
?
>
load more v
88%

Persistent Database Connections , Internally in PHP a connection status is maintained. There are 4 possible states: , HTTP authentication with PHP , One thing to note is that both the ABORTED and the TIMEOUT states can be active at the same time. This is possible if you tell PHP to ignore user aborts. PHP will still note the fact that a user may have broken the connection, but the script will keep running. If it then hits the time limit it will be aborted and your shutdown function, if any, will be called. At this point you will find that connection_status() returns 3.

When a PHP script is running normally, the NORMAL state is active. If the remote client disconnects, the ABORTED state flag is turned on. A remote client disconnect is usually caused by users hitting their STOP button. If the PHP-imposed time limit (see set_time_limit()) is hit, the TIMEOUT state flag is turned on.

You can decide whether or not you want a client disconnect to cause your script to be aborted. Sometimes it is handy to always have your scripts run to completion even if there is no remote browser receiving the output. The default behaviour is however for your script to be aborted when the remote client disconnects. This behaviour can be set via the ignore_user_abort php.ini directive as well as through the corresponding php_value ignore_user_abort Apache httpd.conf directive or with the ignore_user_abort() function. If you do not tell PHP to ignore a user abort and the user aborts, your script will terminate. The one exception is if you have registered a shutdown function using register_shutdown_function(). With a shutdown function, when the remote user hits his STOP button, the next time your script tries to output something PHP will detect that the connection has been aborted and the shutdown function is called. This shutdown function will also get called at the end of your script terminating normally, so to do something different in case of a client disconnect you can use the connection_aborted() function. This function will return true if the connection was aborted.

php_value ignore_user_abort
load more v
72%

How to call functions after previous function is completed in jQuery ?,var callerFunction = function(){ alert("Execution point!"); }; setTimeout(callerFunction, 4000);,How to disable a button in jQuery dialog from a function ?,function callerFunction(){ alert("jQuery setTimeOut execution is fine!"); } setTimeout(callerFunction, 4000);       Or

$.ajax(page_url);
load more v
65%

For more examples check out the Sending and Receiving Binary Data page, Note: You need to add the event listeners before calling open() on the request. Otherwise the progress events will not fire. , Note: Progress events are not available for the file: protocol. , One can also detect all three load-ending conditions (abort, load, or error) using the loadend event:

function reqListener() {
   console.log(this.responseText);
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "http://www.example.org/example.txt");
oReq.send();
load more v
75%

I want to check availability of internet connection before every AJAX request. If the connection is available – proceed else give a pop up message- net not available.,@Suresh, that’s PHP code you’re trying to run inside of JavaScript. $_SERVER is only available to PHP so you’re getting ‘undefined’, Will Permalink to comment# July 11, 2011 @Suresh, that’s PHP code you’re trying to run inside of JavaScript. $_SERVER is only available to PHP so you’re getting ‘undefined’ ,I wrote the javascript to check net connection.But to check if a request is a AJAX request or a normal request if put following filter in my javaScript code:

The HTTP_X_REQUESTED_WITH header is sent by all recent browsers that support AJAX requests.

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
   # Ex.check the query and serve requested data
}

Other "connection-detect" queries related to "AJAX: Detect connection closed, PHP continues"