Delete $_Session when logging out of browser

Asked

Viewed 64 times

0

Hey, guys, I’m making a panel of queries with Sesssions systems. I have a little doubt about him... How can I detect if the user has closed the browser? I already tried UNLOAD(); however it detects when the user changes pages, but I don’t want to. I just want to DETECT if the USER has left the BROWSER so I can QUIT (log out) SESSION.

Follow the code:

    <?php
  //starta a sessão
  session_start();
  ob_start();

  //resgata os valores das session em variaveis
  $id_users = isset($_SESSION['id_users']) ? $_SESSION['id_users']: ""; 
  $nome_user = isset($_SESSION['nome']) ? $_SESSION['nome']: "";  
  $login_users = isset($_SESSION['login']) ? $_SESSION['login']: "";  
  $pass_users = isset($_SESSION['pass']) ? $_SESSION['pass']: ""; 
  $logado = isset($_SESSION['logado']) ? $_SESSION['logado']: "N";
  $tipo_conta = isset($_SESSION['tipo_conta']) ? $_SESSION['tipo_conta']: ""; 
  $pontos = isset($_SESSION['pontos']) ? $_SESSION['pontos']: ""; 
  // Pega dados novamente porem diretamente ao MYSQL
  $pdo = new PDO('mysql:host=localhost;dbname=central_srblue', 'root', '');
  $db = new mysqli('localhost', 'root', '', 'central_srblue');
  $consulta = $pdo->query("SELECT * FROM users WHERE id_users='$id_users'");
  $dados = $consulta->fetch(PDO::FETCH_ASSOC);

  $nome_user = $dados['nome'];
  $tipo_conta = $dados['tipo_conta'];
  $pontos = $dados['pontos'];

  //varificamos e a var logado contém o valos (S) OU (N), se conter N quer dizer que a pessoa não fez o login corretamente
  //que no caso satisfará nossa condição no if e a pessoa sera redirecionada para a tela de login novamente
  if ($logado == "N" && $id_users == ""){     
    echo  "<script type='text/javascript'>
          location.href='index.php'
        </script>"; 
    exit();
  }

?>
  • One possibility of detection would be for the client to send a message indicating that the browser has been closed or for the server to send the user a Session only cookie. But you cannot depend on the user to close the browser to terminate a script, or even determine an action. In the case of the message a power outage, machine breakdown or software failure would prevent the sending of the message. In the case of Session only cookie the current browsers have a functionality continuar de onde parei... that restore Session only cookie which may confuse your application.

  • So the only way is to create COOKIES? but these COOKIES can bug the right panel? Oky will adhere to the same points system, only access such function if you have certain points... Thanks little brother!

  • When you close the browser, the SESSIONS are automatically deleted.

  • and The Data of the Seventh? Because when I log out delete all data and exit Session, only allowed access to the panel when I have the data in SESSION.

No answers

Browser other questions tagged

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