Allow multiple requests from the same PHP user [closed]

0

Hello, I'm working on a legacy system developed with PHP. This system works as a single-page application using jquery.ajax to load the pages that the user requests, each page requested to the system is loaded into a new tab. On search pages depending on the filter by the user the processing may take something around 30 or 40 seconds to return due to the volume data in the database. The problem occurs in this case quoted above where the user can not do anything else in the system until the search request is completed. If the user tries for example to open another page he can not because the system is still processing the previous request. I would like to know if there is any way for PHP to allow multiple requests from a user, in this way the system will allow the user to load other pages while the search is requested.

javascript used in the request

        $.ajax({
            type: 'post',
            url: 'php/incluir_pesquisa.php',
            async: true,
            data: {
                tabID: documentoId,
                nomeDocumento: nomeDocumento
            },
            success: function (result) {
                $('#jqxTabs').jqxTabs(
                    'addLast',
                    'Pesquisar',
                    result
                );
            }
        });
    
asked by anonymous 18.09.2016 / 18:06

2 answers

1

This is probably because of SESSION or TRANSACTION .

PHP does not limit the number of concurrent connections (NGINX, Apache, or other extensions or other firewalls).

1. Session:

Session can not be accessed by more than one process in write-read mode. You can read through countless processes, but you can not write.

Logo:

session_start();

$salvaSession = $_SESSION['ultimo_acesso'] = time();
$lerSession = $_SESSION['usuario'];

 // Seu código demorado....
while(1 = 1){ // Um loop infinito para simbolizar algo demorado :P
}

If you make a request, it will prevent any other requests from being accepted while the loop is closed.

This is because session_start indicates that the session can be read and written, which prevents access from any other process.

So, to fix use:

session_start();

$salvaSession = $_SESSION['ultimo_acesso'] = time();

session_write_close();

$lerSession = $_SESSION['usuario'];

 // Seu código demorado....
while(1 = 1){ // Um loop infinito para simbolizar algo demorado :P
}

Ready, from the "third" line you already allow another process to have access to the same session. Because you say to PHP: "I will not write anything more in this session". You will still be able to read after using session_write_close(); . ;)

Then after session_write_close(); the other request can be processed, which does not require finishing the loop. : D

2. TRANSACTION:

This is more complex and is related to the database (not to PHP). Depends on the level of solos used in the database or queries.

For example:

mysqli_begin_transaction($con);
mysqli_autocommit($con, false);

mysqli_query($con, 'SELECT * FROM usuarios WHERE id = 1 FOR UPDATE');

 // Seu código demorado....
while(1 = 1){ // Um loop infinito para simbolizar algo demorado :P
}

mysqli_commit($sql);

If you go back to this page, it will not be processed on a second tab, because MySQL will prevent the query from being read while there is no UPDATE or COMMIT , this will depend on Engine used, Banco de dados used and the level of 'Isolation!'

    
19.09.2016 / 22:31
0

Would not you like to put some action while loading the requisitions?

$.ajax({
        type : 'POST',
        url  : 'suaPagina.php',
        data : data,
        dataType: 'json',
        beforeSend: function()
        {   
            $(".item").html(''); //<-- ação aqui

        },
    }); 
    
18.09.2016 / 18:12