title: PHP: active Users
body: <p>I just faced a problem. I am currently working on a project for school and I want to have all active users in the Sidebar (similar to a chat, but it's not).</p>
  
  <p>First I thought of this (Pseudocode):</p>
  
  <pre><code>&lt;?php
      ...
      class user {
          //Some instance vars.
  
          public function __construct(){
              //Write username in a textfile/database
          }
          public function logout {
              //Delete username from textfile/database
          }
      }
      ...
  ?&gt;
  </code></pre>
  
  <p>The problems are:</p>
  
  <p>1) The user-object is stored in the <code>$_SESSION[]</code> Array. So I can't use the magic method <code>__destruct()</code>. That's why I made the <code>logout()</code> Method. (Problem solved ^^)</p>
  
  <p>2) If we assume that the user is logged in on 2 devices (eg. Laptop, Smartphone) with the same ID and he quits the session on one device, the other device will also be removed.</p>
  
  <p>3) The user probably doesn't want to press the logout but and just closes the browser window. How do I know, that he logged out / quit the session.</p>
  
  <p>I know, that PHP is not the best programming language for this kind of task, but I really want to do this project in PHP. I am really sorry, if this question has already been asked, but I carefully searched through all related questions and all of them differ (more or less) from mine.</p>
  
  <p>Thanks in advance :)</p>