Redirect to another page

Asked

Viewed 1,209 times

2

Colleagues.

How I would make the user when closing the browser or tab, was directed to another page, but that did not work when F5. I say this because I have a system that has online and offline status and many of them are closing through the browser or tab and the status is still online. The idea is when to close the browser or tab, to be directed to a page where I could disconnect it from the system and then close the browser.

Looking from here and there, I found this code, but it’s not working, but it can be a starting point... look:

<script>
var check;
function fechasess(url,e) {

if (url == 'f5') { //Entra quando uma tecla é apertada

if (e.keyCode == 116) { check = 1; } //Verifica se a tecla é F5 no IE, se for bloqueia o logout

if (e.which == 116) { check = 1; } //Verifica se a tecla é F5 no FF, se for bloqueia o logout

}else if (url == 'logout.php') { //Entra quando o onunload ativa

if (check != 1) { document.location=(url); } //Verifica se deve ativar o logout ou não
}else{ //Entra quando for um link
check = 1; //Bloqueia o logout 

document.location=(url); //Redireciona o link
}
}
</script>
<body onkeydown="fechasess('f5',event)" onkeypress="fechasess('f5',event)" onbeforeunload="fechasess('logout.php')" onunload="fechasess('logout.php')"> 
  • 1

    It seems you want something impossible, but I don’t know if I got it right. You can only intercept the output of the page by events unload and beforeunload, and they rotate both when closing the tab or window, and when reloading the page. But I didn’t quite understand the idea of redirecting when the person tries to close the browser. If it will be closed, it wouldn’t do any good to redirect. And obviously a page can’t stop anyone from closing the browser.

  • rs rs... I thought that with jquery or ajax nothing was impossible... but come on... with jquery I found this code: $(window). Unload(Function(){ Alert("Goodbye!"); }); I also found this code: <body onbeforeunload="Return myFunction()"> But I only need to disconnect the system user, put a url('quit.php') for example, just read this file and continue executing the closing of the page...

1 answer

3

The ideal would be not redirect them, but make a request via ajax to this page and from there update the status.

To intercept when the browser is closing and make an AJAX request, one can use the following code:

window.onunload = function(ev) {
  var request = new XMLHttpRequest();
  request.open('POST', '/caminho/para/update/do/status.php', true);
  request.send();
  request.onreadystatechanged = function(e) {
     if(this.status === 200 || this.statusText === 'OK') {
       // deu tudo certo
     } else {
       // muito provável que algo tenha dado errado
     }
  }
}

Although it is not 100% necessary, jQuery could also be used in place of Vanillajs.

  • Hello thepanuto...no body I put something or just this code?

  • not required... on the page being closed, only a script tag with that code inside. On your page caminho/para/update/do/status.php there has to be a PHP code to update the status, from online to offline.

  • Almost. RRSRS.. I can now change the status, but what is happening is that if I exit the browser, other people end up losing the session too... I changed line request.open('POST', 'exit.php', true); ...

  • There’d be some way it wouldn’t happen?

  • Make sure you’re not changing anyone else’s status by updating the page sair.php.

  • In leaving.php I take the session of each user, which I believe runs only in his browser, but still lets others down. Then I tried querystring each user’s variable to force the individual shutdown: quit.php? Key=$idUsuarios; ... but it didn’t work either...

  • Using $_SESSION, you have access only to the user session that is currently viewing the page. To pass data to the page, send a string query to .send of the object, e.g. request.send('keys=1,2,3');

  • Hello thepanuto. Forgive me ignorance once again. In that case I would have to get into shape? " request.send(<?php echo $_SESSION['Idusuarios']; ?>)"... o e.g coloco antes do request.send?

  • yes, but in this case it would have to be a query string, that is, in the format 'key1=foo&key2=bar'. Before the .send, comes the whole code of my answer, but instead of the .send() empty, you pass the result of php, e.g. .send('<?php echo $minhaVar ?>') and before, update the variable $minhaVar.

  • Okay... I just couldn’t figure out e.g.. rs ... would it look like this? e.g.request.send(...)?

  • e.g. is Latin for "exempli gratia", which means "for example", only. The request code is as it is in the answer, changing only the request.send, that stays that way: request.send('<?php echo $parametros ?>')

  • right thepanuto... was to test here... thanks for the help...

Show 7 more comments

Browser other questions tagged

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