title: Check if a user is online codeigniter without using sessions using time() instead
body: <p>When my user logs in it updates the last_active and last_visit width <code>time()</code>. </p>
  
  <p>Once user has logged on any page that is visits it updates the users <strong>last_active</strong> and <strong>last_visit</strong> column.</p>
  
  <p>I am trying to figure out the best way so on the users profile page it can show if user is online or not even if in different browser.</p>
  
  <pre><code>http://sample.com/user/1
  </code></pre>
  
  <p>I can not seem to get it correct. It still shows user as online in other browsers when infact the user has logged out</p>
  
  <blockquote>
    <p><strong>Question</strong> how can I check if the user is online or not even if in a
    different browser?</p>
  </blockquote>
  
  <p>I have looked at <a href="https://stackoverflow.com/questions/8026263/how-to-see-if-a-user-is-online-in-a-website-with-php-and-mysql-driven-databases">How to see if a user is online in a website with php and mysql driven databases?</a></p>
  
  <p>And <a href="https://stackoverflow.com/questions/20504344/php-check-if-a-user-is-online">PHP Check if a user is online</a></p>
  
  <p>The links not help.</p>
  
  <pre><code>if(date("H:i:s", $udata['last_active']) == date("H:i:s", $udata['last_visit']) &amp;&amp; date("H:i:s", $udata['last_active']) != date("H:i:s", strtotime("+1 min"))) {
      $this-&gt;data['active'] = 1;
  } else {
      $this-&gt;data['active'] = 0;
  }
  </code></pre>
  
  <p>Model </p>
  
  <pre><code>&lt;?php 
  
  class User_model extends CI_Model {
  
  public function __construct() {
      parent::__construct();
  
          if ($this-&gt;islogged() == 1) {
  
              $this-&gt;db-&gt;where('user_id', $this-&gt;getuserid());
              $query = $this-&gt;db-&gt;get($this-&gt;db-&gt;dbprefix  . 'user_data');
  
              if ($query-&gt;num_rows() == 1) {
  
                  $user_data = array(
                      'last_active' =&gt; time(),
                      'last_visit' =&gt; time(),
                      'ip_address' =&gt; $this-&gt;input-&gt;ip_address()
                  );
  
                  $this-&gt;db-&gt;where('user_id', $query-&gt;row()-&gt;user_id);
                  $this-&gt;db-&gt;update($this-&gt;db-&gt;dbprefix . 'user_data', $user_data);
              }
  
          } else {
  
              $this-&gt;logout();
          }
      }
  
  }
  </code></pre>
  
  <p>Controller </p>
  
  <pre><code>&lt;?php
  
  class User extends MY_Controller {
  
      public $data;
  
      public function __construct() {
          parent::__construct();
          $this-&gt;load-&gt;library('elapsed_time');
          $this-&gt;load-&gt;model('forum/thread_model');
          $this-&gt;load-&gt;model('user/user_model');
      }
  
      public function index($user_id) {
          $udata = $this-&gt;user_model-&gt;getuser($user_id);
  
          if ($udata) {
  
              // Titles
              $this-&gt;data['title'] = 'Profile of ' . $udata['username'];
              $this-&gt;data['heading_title'] = 'Forum Info';
  
              // Page Data
              $this-&gt;data['profile_username'] = $udata['username'];
  
              if(date("H:i:s", $udata['last_active']) == date("H:i:s", $udata['last_visit']) &amp;&amp; date("H:i:s", $udata['last_active']) != date("H:i:s", strtotime("+1 min"))) {
                  $this-&gt;data['active'] = 1;
              } else {
                  $this-&gt;data['active'] = 0;
              }
  
              $this-&gt;data['how_long_was_online'] = 'Created account but not logged in yet!';
              $this-&gt;data['date_created'] = date('d-m-Y h:i:s' , $udata['date_created']);
  
              $this-&gt;data['avatar'] = ($udata['avatar']) ? $udata['avatar'] : 'holder.js/100px180';
  
              $this-&gt;data['total_questions'] = $this-&gt;thread_model-&gt;total_users_thread_questions($user_id);
              $this-&gt;data['total_threads'] = $this-&gt;thread_model-&gt;total_users_threads($user_id);
  
              // Page Links
              $this-&gt;data['users_questions'] = site_url('search/questions/' . $user_id);
              $this-&gt;data['users_threads'] = site_url('search/threads/' . $user_id);
              $this-&gt;data['pm'] = site_url('mail/pm/' . $user_id);
              $this-&gt;data['email'] = site_url('mail/email/' . $user_id);
  
              $this-&gt;data['children'] = array(
                  'common/header',
                  'common/footer',
                  'common/navbar' 
              );
  
              $this-&gt;load-&gt;render('user/user', $this-&gt;data);
  
          } else {
  
              redirect('not_found');
          }
      }
  }
  </code></pre>