0

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.

Iyas
  • 520
  • 1
  • 11
  • 40

2 Answers2

0

$scope.submitForm().then(function(data){
   console.log(data);
   window.location.href = "index.php";
});

$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('DataTable Response');
                return ldap.promise;
            }
        });

    $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('LDAP response');
                return userTable.promise;
            }
        });
};

Use this .. passing ldap.resolve('DataTable Response'); data inside resolve() is optional. if you want to pass some message then use.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
0

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();

Community
  • 1
  • 1
Iyas
  • 520
  • 1
  • 11
  • 40