PHP - Real Time Application

Asked

Viewed 428 times

3

A doubt

I need to make an application that from time to time validates whether a session is valid through a bank query. This query must be done automatically, without the need for a user action, type a JS Location.Reload().

I thought about using a php socket to do this validation, but I don’t know if it’s the right one. Or change the whole idea and abandon php and use the nodejs that seems to be more useful for this feature.

Could someone explain to me the best way to develop this application? I only need a guide of what to use, and not of code or scripts ready, it is more the conceptual question itself and show the correct path to follow

  • 1

    How long will you do this validation? If it’s something like <= every 1 minute I would consider using sockets. Now if we say you want to validate every 10 minutes I would stay with ajax.

  • Exactly, 1 in 1 minute would be the ideal time, because I have to validate if the user has certain permissions still valid in the system, in case I do not have I will redirect it to a login page again.

1 answer

1

Follow the outline below in js and php.

I don’t see the need to use socket or nodejs, when a simple request and a setInterval can solve your problem.

Taken 5 seconds is called the Ssao() function, which makes an ajax request where it returns an array containing the result of an sql query;

JS:

$(document).ready(function () {
    setInterval(validaSessao , 5000);
});
function validaSessao(){
    $.ajax({
        method:'post',
        url: 'validaSessao.php',
        async: true,
         dataType:'json',
        success: function (retorno) {

            console.log(retorno);
        }


    });
}

PHP:

$stmt = $conn->prepare("");
$stmt->bindParam('', $nome, PDO::PARAM_STR);
$res = $stmt->execute();
echo json_encode($res);
  • 1

    I do not believe it is the ideal to consume resources from time to time in this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.