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

标签 php mysql pdo phpmyadmin timestamp

几天来我一直在寻找这样一个非常简单的计数器,它可以显示我网站上当前登录的用户数量 - 然而,我没有找到任何东西。我发现的东西都与已弃用的 *mysql_functions.... :( - 叹气...

我正在寻找的是如何使这项工作发挥作用,因为我真的无法弄清楚 - 人们会认为这很容易,但天哪,在搜索和搜索之后,似乎有很多不同的方法可以完成遗憾的是,我发现的一切都是我上面提到的(使用已弃用的 mysql_functions 的东西)...

无论如何,这就是我想要做的:

1.) 添加lastactivitylastseen在我的 users基于时间戳或可能是 tinyint(1) 的表,默认值为 0,等于用户注销,如果用户登录,则将切换为 1...

2.) 根据我的数据库表 users 中的内容,使用带有 PDO 驱动程序的 php 版本 (5.4.24) 为此创建一个类这将更新并选择我的数据库表中的内容 users每 5-10 分钟输出一次当前登录的用户数。

3.) 能够使用<?php echo $lastactivity; ?><?php echo $lastseen; ?>为了在每个页面上显示当前登录的用户数,我将这段代码放在...

4.) 如果可能的话,所有这些都试图避免基于 SESSIONS...

如果有人能阐明如何实现这一目标,我将不胜感激!

更新:

我的 users 会是什么表:

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

我现在的 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();
    }
}

在每个页面上显示当前登录用户数的内容:

或者<?php echo $lastseen; ?><?php echo $lastactivity; ?>

更新 2:

包括我的注销功能:

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();
        }
    }
}

更新 3:

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

最佳答案

  1. 我们将列称为“loggedIn”,而不是“lastseen”。这样, bool 值 0 或 1 就有意义了。扩展您现有的登录功能,将 loggedIn 从 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;
        }
    

显然,您需要在注销函数中使用类似的语句将值设置回 0。

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());
    }
}

关于php - 使用 php、pdo 和 mysql 显示当前登录用户的简单计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21487341/

相关文章:

mysql - SQL VIEW简化/解决更快的查询

php - IP冲突mysql

php - 将带有 Mysql ext 的 php 代码转换为 PDO 不起作用

php - 数据库设计一对一关系

php PDO 准备好的语句不能按预期工作

php - 多维数组的AJAX POST-用PHP写入MYSQL数据库

php - 如何从 MySQL 获取并组合数据,然后插入到 PHP 数组中

php - 为内部联接查询中的一个字段选择不同的值

php - 如何从外部js文件调用php文件中的函数

php - 如果表中不存在 userID,则添加 mysql 行