Iyas Iyas - 7 days ago 6
PHP Question

Cancel request to PHP page results in another PHP page delayed responding

I am developing a login page which look into LDAP and MySQL database for user authentication. The idea is to request two PHP page simultaneously, any one complete request will cancel the other one request.

Here is my code:

$scope.submitForm = function(username, password) {
var ldap = $q.defer();
var userTable = $q.defer();

$http({
timeout: userTable.promise,
method: 'POST',
url: 'crud/00loginUserTable.php',
data: {
username: username,
password: password
}
})
.then(function(response) {
if (response.data.message != "ok")
alert("Tak OK");
else {
sessionStorage.jwt = response.data.jwt;
ldap.resolve();
window.location.href = "index.php";
}
});

$http({
timeout: ldap.promise,
method: 'POST',
url: 'crud/00loginLDAP.php',
data: {
username: username,
password: password
}
})
.then(function(response) {
if (response.data.message != "ok")
alert("Tak OK");
else {
sessionStorage.jwt = response.data.jwt;
userTable.resolve();
window.location.href = "index.php";
}
});
};


This code actually works. BUT...

There is 1.3 minute delay before
window.location.href = "index.php";
could be execute. As I found out, it is something to do with PHP. I tried changing
window.location.href = "index.php";
to
window.location.href = "index.html";
. Viola! No delay. So it seems the
index.php
is waiting for
00loginLDAP.php
to timeout before responding.

I know the problem, but I don't know the solution.

Please help.

Answer

As I learnt here, merely canceling the $http request won't stop the PHP execution. The reason the other PHP page wait until the first PHP page is finish, because both using session_start();.

I quote:

Moreover if you are using PHP session, they are another tricky situation. For example, if you are doing AJAX, and you actually send two AJAX request to a PHP script and that PHP script has need of session with session_start(). The first AJAX query will work normally, however the second one will have to wait until the first call is finish, because the first script has a locked on the session. The first script could eventually prematurely release the session with session_write_close();

Comments