简体   繁体   English

显示使用php,pdo和mysql的当前登录用户的简单计数器

[英]Display a simple counter of currently logged in users using php, pdo and mysql

I have been searching for days for such a very simple counter that displays a number of currently logged in users on my website - however, have had no luck finding anything. 我一直在寻找这样一个非常简单的计数器,该计数器显示了我网站上当前已登录的用户数-但是,运气不好。 And the stuff I have found all relates to the deprecated *mysql_functions.... :( - sigh... 我发现的东西都与不建议使用的* mysql_functions .... :(-叹息...

All I am looking for is how to make this work as I literally cannot figure this out - one would think it is very easy but holy crap, after searching and searching, it seems like there are just so many different ways to accomplish this and sadly everything I have found is what I mentioned above (stuff using the deprecated mysql_functions)... 我所寻找的只是如何使这项工作变得切实可行,因为我从字面上无法弄清这一点-人们会认为这很简单,但是在搜寻之后,似乎有很多不同的方法来完成这项工作,可悲的是我发现的所有内容都是我上面提到的(使用已弃用的mysql_functions的东西)...

Anyway, this is all that I am looking to do: 无论如何,这就是我要做的一切:

1.) Adding lastactivity or lastseen in my users table either based on a timestamp or perhaps a tinyint(1) with default 0 which equals user logged out of which would switch to 1 if user is logged in... 1.)根据时间戳或默认值为0的tinyint(1)在我的users表中添加lastactivitylastseen ,这等于用户注销,如果用户登录则将切换为1 ...

2.) Creating a class for this using php version (5.4.24) with PDO driver based on what is in my database table users which would update and select what is in my database table users every 5-10 minutes which would output the number of currently logged in users. 2)创建一个类此使用PHP版本(5.4.24)与PDO驱动基于什么是我的数据库表users ,其将更新并选择是什么在我的数据库表users每5-10分钟这将输出数量当前登录的用户数。

3.) Being able to use <?php echo $lastactivity; ?> 3.)能够使用<?php echo $lastactivity; ?> <?php echo $lastactivity; ?> or <?php echo $lastseen; ?> <?php echo $lastactivity; ?><?php echo $lastseen; ?> <?php echo $lastseen; ?> to display the number of users currently logged in on each page I place this piece of code on... <?php echo $lastseen; ?>显示每个页面上当前登录的用户数,我将这段代码放在...上

4.) All of which trying to stay away from basing this on SESSIONS if possible... 4.)如果可能的话,所有这些都试图避免以此为基础...

If anyone could shed some light on how to accomplish this, I would be very grateful! 如果有人能阐明如何实现这一目标,我将不胜感激!

UPDATE: 更新:

what would be in my users table: 我的users表中将是什么:

lastseen | tinyint(1) | Default 0
lastactivity | timestamp | Default CURRENT_TIMESTAMP

what I currently have in my users.php class page - I'll display everything I have atm so be prepared for a lot - nothing pertaining to a counter of logged in users is in this yet, however, with what I do have I hope will shed some light on how to add what I need to it...: 我在users.php类页面中当前拥有的内容-我将显示我拥有的atm内容,因此请做好很多准备-但是,与已登录用户的计数器无关,但是,我希望这样做将阐明如何添加我需要的东西...:

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`    = ?

                                WHERE `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 from what is above to suit needs. Without this update, status update does not work in logster.php!
    public function update_status($status, $id){

        $query = $this->db->prepare("UPDATE `users` SET
                                `status`        = ?
                                WHERE `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` = ? WHERE `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 are added to 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 FROM `users` WHERE $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`) FROM `users` WHERE `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` FROM `users` WHERE `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 * FROM `users` WHERE `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 * FROM `users` ORDER BY `time` DESC");

        try{
            $query->execute();
        }catch(PDOException $e){
            die($e->getMessage());
        }

        return $query->fetchAll();
    }
}

What would be included on each page to display number of currently logged in users: 每个页面上将显示什么以显示当前登录用户的数量:

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

UPDATE 2: 更新2:

Including my logout function: 包括我的注销功能:

class General{
        // Note: Created to specify what logged in users can see/view and edit/update
    public function logged_in () {
        return(isset($_SESSION['id'])) ? true : false;
    }
        // Note: Created to specify what a specific individual or user can see/view and edit/update
    public function logged_in_protect() {
        if ($this->logged_in() === true) {
                header('Location: home.php');
            exit();
        }
    }
        // Note: Created to specify what non-users - (those who are not logged in and/or do not have an account) can see/view only - (edit/update is unavailable)
    public function logged_out_protect() {
        if ($this->logged_in() === false) {
            header('Location: index.php');
            exit();
        }
    }
}

UPDATE 3: 更新3:

session_start();
session_destroy();
header('Location:index.php');
  1. Instead of "lastseen", let's call the column "loggedIn". 代替“ lastseen”,我们将其称为“ loggedIn”列。 That way, a boolean 0 or 1 makes sense. 这样,布尔值0或1就有意义。 Expand your existing login function to change loggedIn from 0 to 1: 扩展现有的登录功能,将loginIn从0更改为1:

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

Obviously you'll need a similar statement in the logout function to set the value back to 0. 显然,您需要在注销函数中使用类似的语句将值设置回0。

2 & 3. You could then add this as an additional function of the Users class: 2&3。然后可以将其添加为Users类的附加功能:

public function usersLoggedIn() {
    $query = $this->db->prepare("SELECT COUNT(*) AS loggedInCount FROM users 
                                 WHERE loggedIn = 1");
    try {
        $query->execute();

        while ($row = $query->fetch()) {   
            $loggedIn = $row['loggedInCount'];
        }
        return $loggedIn;

    } catch(PDOException $e) {
        die($e->getMessage());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM