Welcome Guest, Not a member yet? Register   Sign In
sessions and authentication
#7

[eluser]xwero[/eluser]
i took the code from the article Detecting Users Online and used it to create a model to check if the user is logged in.
Code:
class Usersmodel extends Model
{
    
    var $visit_timeout = 600;
    
    function Usersmodel()
    {
        parent::Model();
    }
    
    function track_user()
    {
      
       $name = $this->session->userdata('username');
       // changed
       /*$name = "";
       if(isset($_SESSION['username'])) {
          // user is logged in so track the username
          $name = $_SESSION['username'];
       } else {
          // user is not logged in so track the IP
          $name = $_SERVER['REMOTE_ADDR'];
       }*/
    
       $time = time();
      
       // Cleanup old visits
       $this->db->where('timestamp <=',$time-$this->visit_timeout);
       $this->db->delete('userOnline');
       /*$query = "DELETE FROM `userOnline` WHERE `timestamp` <= '"
          .($time-VISIT_TIMEOUT)."';";
       @mysql_query($query);*/
    
       // check if user is already listed
       $this->db->where('title',$name);
       $this->db->from('userOnline');
       $count = $this->db->count_all_results();
       /*$query = "SELECT COUNT(*) AS total FROM `userOnline` ".
                     "WHERE `title`='$name';";
       $result = @mysql_query($query, $db);
       if($result = @mysql_fetch_array($result)) {
          $count = $result['total'];
       } else {
          // Could not get a record from the result, so must be 0
          $count = 0;
       }*/
      
       // If already visitor then update, otherwise add them
       // changed
       if($count > 0) {
               return true;
       }
       else
       {
               $this->db->set('timestamp',$time);
               $this->db->set('title',$name);
               $this->db->insert('userOnline');
               return false;
       }
       /*if($count > 0) {
          $query = "UPDATE `userOnline` SET `timestamp`='".$time.
                     "' WHERE title='$name';";
       } else {
          $query = "INSERT INTO `userOnline` (`title`, `timestamp`) ".
                     "VALUES ('$name', '$time');";
       }
       @mysql_query($query, $db);*/
    }
    
}
I left the original code in for educational purpose Smile Change the code to your needs


Messages In This Thread
sessions and authentication - by El Forum - 02-13-2008, 09:51 AM
sessions and authentication - by El Forum - 02-14-2008, 04:50 AM
sessions and authentication - by El Forum - 02-15-2008, 02:10 AM
sessions and authentication - by El Forum - 02-15-2008, 02:29 AM
sessions and authentication - by El Forum - 02-15-2008, 04:20 AM
sessions and authentication - by El Forum - 02-15-2008, 05:11 AM
sessions and authentication - by El Forum - 02-15-2008, 05:14 AM
sessions and authentication - by El Forum - 02-15-2008, 05:18 AM
sessions and authentication - by El Forum - 02-15-2008, 05:24 AM
sessions and authentication - by El Forum - 02-15-2008, 05:52 AM
sessions and authentication - by El Forum - 02-15-2008, 06:16 AM
sessions and authentication - by El Forum - 02-15-2008, 06:36 AM
sessions and authentication - by El Forum - 02-15-2008, 06:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB