Display a simple counter of currently logged in users using php, pdo and mysql -


i have been searching days such simple counter displays number of logged in users on website - however, have had no luck finding anything. , stuff have found relates deprecated *mysql_functions.... :( - sigh...

all looking how make work literally cannot figure out - 1 think easy holy crap, after searching , searching, seems there many different ways accomplish , sadly have found mentioned above (stuff using deprecated mysql_functions)...

anyway, looking do:

1.) adding lastactivity or lastseen in users table either based on timestamp or perhaps tinyint(1) default 0 equals user logged out of switch 1 if user logged in...

2.) creating class using php version (5.4.24) pdo driver based on in database table users update , select in database table users every 5-10 minutes output number of logged in users.

3.) being able use <?php echo $lastactivity; ?> or <?php echo $lastseen; ?> display number of users logged in on each page place piece of code on...

4.) of trying stay away basing on sessions if possible...

if shed light on how accomplish this, grateful!

update:

what in users table:

lastseen | tinyint(1) | default 0 lastactivity | timestamp | default current_timestamp 

what have in users.php class page - i'll display have atm prepared lot - nothing pertaining counter of logged in users in yet, however, have hope shed light on how add need it...:

class users {      private $db;      public function __construct($database) {         $this->db = $database;     }      public function update_user($clan_tag,                     $gamer_tag,                     $gender,                     $day,                     $month,                     $year,                     $location,                     $occupation,                     $interests,                     $bio,                     $status,                     $xfire,                     $steam,                     $image_location,                     $id) {          $query = $this->db->prepare("update `users` set                                 `clan_tag`      = ?,                                 `gamer_tag`     = ?,                                 `gender`        = ?,                                 `day`           = ?,                                 `month`         = ?,                                 `year`          = ?,                                 `location`      = ?,                                 `occupation`        = ?,                                 `interests`     = ?,                                 `bio`           = ?,                                 `status`        = ?,                                 `xfire`         = ?,                                 `steam`         = ?,                                 `image_location`    = ?                                  `id`      = ?");          $query->bindvalue(1, $clan_tag);         $query->bindvalue(2, $gamer_tag);         $query->bindvalue(3, $gender);         $query->bindvalue(4, $day);         $query->bindvalue(5, $month);         $query->bindvalue(6, $year);         $query->bindvalue(7, $location);         $query->bindvalue(8, $occupation);         $query->bindvalue(9, $interests);         $query->bindvalue(10, $bio);         $query->bindvalue(11, $status);         $query->bindvalue(12, $xfire);         $query->bindvalue(13, $steam);         $query->bindvalue(14, $image_location);         $query->bindvalue(15, $id);          try{             $query->execute();         }catch(pdoexception $e){             die($e->getmessage());         }     }     // update_status below has been duplicated above suit needs. without update, status update not work in logster.php!     public function update_status($status, $id){          $query = $this->db->prepare("update `users` set                                 `status`        = ?                                 `id`      = ?");          $query->bindvalue(1, $status);         $query->bindvalue(2, $id);          try{             $query->execute();         }catch(pdoexception $e){             die($e->getmessage());         }     }      public function change_password($user_id, $password) {          global $bcrypt;          $password_hash = $bcrypt->genhash($password);          $query = $this->db->prepare("update `users` set `password` = ? `id` = ?");          $query->bindvalue(1, $password_hash);         $query->bindvalue(2, $user_id);          try{             $query->execute();             return true;         } catch(pdoexception $e){             die($e->getmessage());         }     }           public function fetch_info($what, $field, $value){         /* (add more here if new columns/rows added table) */         $allowed = array('id',                  'username',                  'clan_tag',                  'gamer_tag',                  'gender',                  'day',                  'month',                  'year',                  'location',                  'occupation',                  'interests',                  'bio',                  'status',                  'xfire',                  'steam',                  'email');          if (!in_array($what, $allowed, true) || !in_array($field, $allowed, true)) {             throw new invalidargumentexception;         }else{              $query = $this->db->prepare("select $what `users` $field = ?");              $query->bindvalue(1, $value);              try{                  $query->execute();              } catch(pdoexception $e){                  die($e->getmessage());             }              return $query->fetchcolumn();         }     }       public function user_exists($username) {          $query = $this->db->prepare("select count(`id`) `users` `username`= ?");         $query->bindvalue(1, $username);          try{              $query->execute();             $rows = $query->fetchcolumn();              if($rows == 1){                 return true;             }else{                 return false;             }          } catch (pdoexception $e){             die($e->getmessage());         }     }      public function login($username, $password) {          global $bcrypt;          $query = $this->db->prepare("select `password`, `id` `users` `username` = ?");         $query->bindvalue(1, $username);          try{              $query->execute();             $data               = $query->fetch();             $stored_password        = $data['password'];             $id                 = $data['id'];              if($bcrypt->verify($password, $stored_password) === true){                 return $id;             }else{                 return false;             }          }catch(pdoexception $e){             die($e->getmessage());         }     }      public function userdata($id) {          $query = $this->db->prepare("select * `users` `id`= ?");         $query->bindvalue(1, $id);          try{              $query->execute();              return $query->fetch();          } catch(pdoexception $e){              die($e->getmessage());         }     }      public function get_users() {          $query = $this->db->prepare("select * `users` order `time` desc");          try{             $query->execute();         }catch(pdoexception $e){             die($e->getmessage());         }          return $query->fetchall();     } } 

what included on each page display number of logged in users:

either <?php echo $lastseen; ?> or <?php echo $lastactivity; ?>

update 2:

including logout function:

class general{         // note: created specify logged in users can see/view , edit/update     public function logged_in () {         return(isset($_session['id'])) ? true : false;     }         // note: created specify specific individual or user can see/view , edit/update     public function logged_in_protect() {         if ($this->logged_in() === true) {                 header('location: home.php');             exit();         }     }         // note: created specify non-users - (those not logged in and/or not have account) can see/view - (edit/update unavailable)     public function logged_out_protect() {         if ($this->logged_in() === false) {             header('location: index.php');             exit();         }     } } 

update 3:

session_start(); session_destroy(); header('location:index.php'); 

  1. instead of "lastseen", let's call column "loggedin". way, boolean 0 or 1 makes sense. expand existing login function change loggedin 0 1:

    if($bcrypt->verify($password, $stored_password) === true){         return $id; //update database $query = $this->db->prepare("update `users` set `loggedin` = `1`                               `username` = ?"); $query->bindvalue(1, $username); $query->execute();      }else{         return false;     } 

obviously you'll need similar statement in logout function set value 0.

2 & 3. add additional function of users class:

public function usersloggedin() {     $query = $this->db->prepare("select count(*) loggedincount users                                   loggedin = 1");     try {         $query->execute();          while ($row = $query->fetch()) {                $loggedin = $row['loggedincount'];         }         return $loggedin;      } catch(pdoexception $e) {         die($e->getmessage());     } } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -