Identifying a page exchange in a PHP web system [closed]

-2

When the user enters a certain system page, I update the database, but when the user exits the page I need to pick up the event and do another update, I can not figure out what the event is. user exit the page and go to another, I am using php on ServerSide.

    
asked by anonymous 23.11.2016 / 11:18

2 answers

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 work fully 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 browser's forward or backward function, it will trigger a request to PHP on the server that will do the registration you expect.

    
23.11.2016 / 13:34
-1

I will demonstrate this example and you can adapt it according to your need, see:

In the JS file you put:

window.onbeforeunload = function(e) {

    var url_atual = window.location.href; // Aqui estou pegando a URL atual

    $.ajax({
        url: 'diretoriodoarquivo/nomedoarquivoquefazupdate.php',
        type: 'POST',
        data: { pagina: url_atual } ,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {},
        error: function () {}
    }); 
};

In case this is the function that will communicate with the php file that will perform the update.

In the php file that does the update:

<?php 
    $pagina     = $_GET["pagina"];
    $usuario    = $_SESSION["idusuarioLogado"];

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

    $query = mysql_query("UPDATE usuarios SET url_atual='{$pagina}' WHERE id={$usuario}");

?>
    
23.11.2016 / 13:42