My little application works in this way: in a simple text input user are asked to insert an url. on change my script tries to extract and display the first 10 images found on this page.
<input type="text" name="url" id="url" value="">
$("form.link-form input#url").change(function() {
var request = $.ajax({
type: "GET",
url: "funzioni/ajax/loadImagestFromUrl.php",
data: "url=" + insertedUrl,
dataType: "html",
timeout: 5000,
success: function(res) {
loadUrlImages2div(msg99);
},
error: function() {
request.abort();
}
});
});
the PHP script loadImagestFromUrl.php run this code, using PHP Simple HTML DOM Parser library:
set_time_limit(5);
$html = file_get_html($url); // load into this variable the entire html of the page
$count=1;
foreach($html->find('img') as $key=>$element) { // only images
if ($count==11) break; //only first 10 images
echo "<img class=\"imgFromUrl\" src=\"".$element->src."\" />\n";
}
$count++;
}
This work great on most cases but there are some urls that are not available in few seconds, or protected under password and the server keep executing something even if I set a timeout of 5 seconds for the ajax request and 5 second for the execution of php code.
When this happens everything get blocked, even refreshing the page is impossible, because it load and load and only after a lot of time it returns "504 Gateway Time-out. The server didn't respond in time."
Can somene help me in understanding how to completely block this request and let the server keep working?