How to confirm page output?

0

How can I confirm if the user actually left the page, because when I click on the browser to exit and say that I want to remain it turns the name of my a into 1 and then does not return to 0. Does anyone have a solution? Code below:

<!DOCTYPE html>
<html>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
window.onbeforeunload = closeSession;

function closeSession() {
  if (true) {
    var a = document.getElementById('a');
    if (a.name != 1) {
      $.ajax({
        url: "teste.php",
        type: "GET",
        async: false,
        cache: false,
        data: {
          'nome' : "Rogers2",
          'senha' : "123456"
        }
      });
      return "disconnected";
    }
  } else {
    a.name = 0;
  }
}
</script>
<body>

<p>Close this window, press F5 or click on the link below to invoke the onbeforeunload event.</p>

<a href="teste2.php" onclick="this.name = 1" id="a" name="0">teste2</a>
</body>
    
asked by anonymous 14.07.2015 / 19:19

3 answers

2

To confirm you can use the "confirm" function in javascript


<?php 
session_start();

if ($_POST['action'] == 'logout') {

    unset($_SESSION['nome']);
    unset($_SESSION['senha']);

     /* aqui você coloca o comportamento que 
        você quer no PHP, como gravar dados 
        no seu banco, etc. E retorna um tipo de saída válida, 
        caso tenha efetuado o logout corretamente... */
     $saida = true;

    if ($saida) {
       echo json_encode(array('message'=>'Você saiu do sistema!'));
       die();
    }
}
?>

<script>
sairDaPagina() {
   if (confirm('Tem certeza que deseja sair desta página?')) {
       closeSession();
       location.href = 'teste2.php';
   }
}

function closeSession() {

 $.ajax({
    url: "<?php echo $_SERVER['SCRIPT_NAME']; ?>",
    type: "POST",
    async: false,
    cache: false,
    data:{
        'nome' : "Rogers2",
        'senha' : "123456",
        'action' : "logout"
        }
    })
     .done(function(retn){
         alert(retn.message);
    });

}
</script>
<a href="javascript:sairDaPagina();">Sair</a>

Or if you want something cool, you can do it through a modal bootstrap, for example: link

    
14.07.2015 / 20:45
0

Change the <a href="teste2.php" onclick="this.name = 1" id="a" name="0">teste2</a> tag so that the onclick event only changes the link name if the user confirms that they really want to exit.

It will look like this:

<a href="teste2.php" onclick="if confirm('Tem certeza que deseja sair da página?') {this.name = 1}" id="a" name="0">teste2</a>

To get a cleaner code you can also declare a function inside your script and call it onclik:

function confirmarSaidaDoUsuario(link) {
    if confirm('Tem certeza que deseja sair da página?') {
        link.name = 1;
    }
}

<a href="teste2.php" onclick="confirmarSaidaDoUsuario(this)" id="a" name="0">teste2</a>
    
14.07.2015 / 22:21
0

The best solution to this is to check the last connection of the user, for this you would have to store the date that he did something on the site and thus check if he is ax seconds / minutes / hours without connecting, if he then it is disconnected.

    
09.10.2015 / 18:20