Identifying a page exchange in a PHP web system

Asked

Viewed 97 times

-2

When the user enters a certain page of the system, I do an update in the database, but when the user leaves this page I need to take the event and do another update, I am not able to find out what is the event of the user leave the page and go to another, I’m using php in Serverside.

  • 1

    Your doubt is too wide. Are you trying to control the pages the user browses in the database? Code examples?

  • Yes, the big question would be, how can I intercept user drive between the pages of my system.

1 answer

1

You can try to detect this event from the browser back button with javascript in this way. But I’m not sure if it will fully work for the purpose you need:

var url = "http://localhost/seuaplicativo/updateBanco.php";
function updateBanco(e) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if(this.readyState == 4 && this.status == 200) {
            console.log("Sucesso");
        }
    };
    xhttp.open("GET", url+"?url="+encodeURI(document.location), true);
    xhttp.send();
}
window.onpopstate = updateBanco;

and in PHP do something similar to this:

updateBanco.php

$url = $_GET["url"];

$banco = mysql_query("localhost", "root", "");
$db = mysql_select_db("controle_de_navegacao");

$query = mysql_query("INSERT INTO tabela (id, url) VALUES (NULL, "{$url}")");

echo $query ? "OK" : "OPS";

?>

What happens: When javascript detects the back or forward function of the browser, it will trigger a request to PHP on the server that will make the record you expect.

  • Very complete your answer, however just by clicking on the back button of the browser will not solve my problem.

Browser other questions tagged

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