How to confirm that the page has left?

Asked

Viewed 1,771 times

0

How can I confirm if the user really left the page, because when I click on the browser to exit and say I want to remain it turns the name of mine a 1 and then no longer 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>

3 answers

2

To confirm you can use the javascript "confirm" function


<?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 nicer, you can do it through a modal like bootstrap, for example: http://getbootstrap.com/javascript/#modals

  • I have tried this but failed. I thank you anyway.

  • But explain better what you want exactly so I can help you, because from what I understand in the question, you want an exit confirmation page.

0

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

0

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

She’ll be 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 cleaner code you can also declare a function within your script and call in 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>

Browser other questions tagged

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