all of the below is based on Linux, recent Apache, recent PHP module for Apache.
I am experiencing an unexpected behavior for my AJAX+PHP based web-application. It is quite a simple AJAX approach, basically involving a request to a PHP-script (execute.php) that runs for some time (few seconds to several minutes). As a test-setup, the PHP-script (execute.php) basically just sleeps for a certain amount of time. When this amount is 899 seconds, it works, but when I set it to 900, the answer of the PHP-script (simple echo() ) seems to expire or something like this (what is exactly the cause or reason is actually unknown to me) as the request is not taking notice of it and thus not responding and the respective innerHTML is not updated, as expected. Please find the code below.
I am now wondering where this "timeout" of 900 seconds comes from and even more important, how can I prevent that from happening? After all, I thought that this was, at least, one general idea of AJAX to handle asynchronous calls and notify the client as soon as a request has changed its state?!
process.php
<?php
session_start();
include("functions.php");
include("env.php");
html_head("Processing...");
$sid = $_SESSION['my_sid'];
?>
<script type="text/javascript">
function run_analyses(params){
<?php
print "var sid = \"". $sid ."\";
";
?>
// Use AJAX to execute the programs independenantly in the background
// Allows for the user to close the process-page and come back at a later point to the results-link, w/o need to wait.
if (window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
}
else
{
//Fallback for IE5 and IE6, as these don't support the above writing/code
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
//Is http_request still false
if (!http_request)
{
alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
}
http_request.onreadystatechange=function(){
if (http_request.readyState==4 && http_request.status==200){
// Maybe used to display the progress of the execution
document.getElementById("output").innerHTML=http_request.responseText;
}
};
http_request.open("POST","execute.php",true);
//Send the proper header information along with the request
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", params.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(params);
};
</script>
<?php
$params "some_params";
print "
Starting AJAX-call<br>
<div style=\"width:400px; border: 1px black solid;\" id=\"output\">Use this to display an information about the progress</div>
<script type=\"text/javascript\">
run_analyses('".$params."');
</script>
<form action=\"./usersets/". $sid ."/results.html\" id=\"go_to_results\" method=\"POST\">
<input type='hidden' name=\"session_id\" value=\"" .$sid. "\">
</form>
";
html_foot();
?>
and execute.php:
<?php
session_start();
include("functions.php");
include("env.php");
$sid = $_SESSION['my_sid'];
// Used to get the time at start of processing
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
session_write_close();
sleep(900); //899 here works
session_start();
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 3);
//echo 'Page generated in '.$total_time.' seconds.'."
";
// Make it accessible for the results
$_SESSION['process_time'] = $total_time;
echo("none");
?>
So the text after "Starting AJAX call" is updated to "none" when the PHP-script sleeps for 899 seconds, but stays "Use this to display an information about the progress" if that value is 900 seconds.
I am really looking forward to your suggestions.
Best,
Shadow
EDIT
Doing grep max_execution $(locate php.ini )
gives:
/etc/php5/apache2/php.ini:max_execution_time = 30
/usr/share/doc/php5-common/examples/php.ini-development:max_execution_time = 30
/usr/share/doc/php5-common/examples/php.ini-production:max_execution_time = 30
/usr/share/php5/php.ini-production:max_execution_time = 30
/usr/share/php5/php.ini-production-dist:max_execution_time = 30
/usr/share/php5/php.ini-production.cli:max_execution_time = 30
EDIT2 When calling the scripts locally (apache webserver running on my local linux machine) in my browser interestingly it behaves as expected and respectes the return of the request. But it exhibits the above unexpected behavior when run on a webserver running on a different machine. Any clues on that? Because I don't have any... Sorry.