简体   繁体   English

使用php pdo获取当前的在线用户

[英]Get Currently online users with php pdo

I want to get currently online users with the help of their last login time . 我想借助他们的上次登录时间来吸引当前在线用户。 My timezone in php is "Asia/Karachi". 我在php中的时区是“ Asia / Karachi”。

What i have tried so far: 到目前为止我尝试过的是:

$stmt = $db_con->prepare("SELECT COUNT(*) AS id 
                        FROM users 
                        WHERE last_login_time > DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_rows = $row['id'];
echo total_rows;

Login process php: 登录过程php:

$login_time = date('y-m-d h:i:s');
$stmt =$db_con->prepare("UPDATE users SET last_login_time='".$login_time."',last_login_ip='".$login_ip."' WHERE email='".$user_email."'");
$stmt->execute();

SQL TABLE: SQL表:

在此处输入图片说明

The problem: i want to show the online users in my php file and there is always digit 0 even through my last login time is smaller than 5minutes .but when i change the INTERVAL 5 MINUTE to INTERVAL 50000 MINUTE ,it shows the number of online users. 问题:我想在我的php文件中显示在线用户,即使我的上次登录时间小于5分钟,也总是有数字0。但是当我将INTERVAL 5 MINUTE更改为INTERVAL 50000 MINUTE时,它显示了在线数量用户。 I have tried a lot but not getting to any point! 我已经尝试了很多,但没有任何进展!

Please See UPDATE at the bottom: 请参阅底部的“更新”:

I believe your issue is based on the MySQL using a different time zone to your PHP. 相信您的问题是基于与您的PHP使用不同时区的MySQL。 This will be almost certainly true if your MySQL connection from PHP is not localhost . 如果您通过PHP进行的MySQL连接不是 localhost那么几乎可以肯定是这样

However, even if you are using localhost , you may have somehow miss-set your MySQL to be using a different time zone to your PHP system. 但是,即使您使用localhost ,您也可能会以某种方式将MySQL设置为与PHP系统使用不同的时区。

So first; 所以首先 read this post to get the current timezone of your MySQL, and to output a Hours / Minutes result: 阅读这篇文章以获取MySQL的当前时区,并输出Hours / Minutes结果:

SELECT TIMEP_FORMAT(TIMEDIFF(NOW(), UTC_TIMESTAMP), '%H%i') as MySQLTime

And compare this with your PHP timezone value retrieved from date_default_timezone_get() and your PHP system time: 并将其与从date_default_timezone_get()检索到的PHP时区值以及您的PHP系统时间进行比较:

print date("r");

Once you've compared and found these are different time values then adjust your MySQL time zone using this answer here . 比较并发现这些时间值不同后,请在此处使用此答案调整MySQL时区。

In PHP: 在PHP中:

 <?php define('TIMEZONE', 'Europe/Paris'); date_default_timezone_set(TIMEZONE); 

For MySQL: 对于MySQL:

 <?php $now = new DateTime(); $mins = $now->getOffset() / 60; $sgn = ($mins < 0 ? -1 : 1); $mins = abs($mins); $hrs = floor($mins / 60); $mins -= $hrs * 60; $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins); //Your DB Connection - sample $db = new PDO('mysql:host=localhost;dbname=test', 'dbuser', 'dbpassword'); $db->exec("SET time_zone='$offset';"); 

UPDATE: 更新:

My timezone in php is "Asia/Karachi". 我在php中的时区是“ Asia / Karachi”。

This is your problem. 这是你的问题。 Your MySQL is using UTC and your PHP is using "Asia/Karachi". 您的MySQL使用的是UTC,而您的PHP使用的是“亚洲/卡拉奇”。

So assuming you want to keep your PHP timezone and apply this timezone to your MySQL you need to set the MySQL Timezone, using this excellent answer . 因此,假设您希望保留PHP时区并将此时区应用于MySQL,则需要使用此出色答案设置MySQL时区。

If you can't use the core methods outlined in the link above, then you can do this at script execution time with PDO: 如果您不能使用上面链接中概述的核心方法,则可以在脚本执行时使用PDO进行此操作:

$pdo = new PDO('mysql:host=localhost;dbname=exampletable', 'user', 
       'pass', [PDO::MYSQL_ATTR_INIT_COMMAND =>"SET time_zone = 'Asia/Karachi'"]);

You will need to manually update all timestamps already set in the DataBase but new timestamps added will be added in the correct timezone. 您将需要手动更新数据库中已设置的所有时间戳,但是添加的新时间戳将添加到正确的时区中。

This is most likely because you are using Asia/Karachi as your default timezone in PHP but your database is defaulting to UTC. 这很可能是因为您使用Asia / Karachi作为PHP中的默认时区,但是数据库默认为UTC。 It's better to use UTC both in MySql and PHP, and just use the local timezone for display. 最好在MySql和PHP中都使用UTC,并且只使用本地时区进行显示。

If you must store your dates in Asia/Karachi, you have a few options. 如果您必须将日期存储在亚洲/卡拉奇,则有几种选择。 One would be to set your MySql timezone to Asia/Karachi. 一种方法是将MySql时区设置为Asia / Karachi。 The other would be to do the date arithmetic in PHP. 另一种方法是在PHP中执行日期算术。 So instead you would do: 因此,您可以这样做:

$five_min_ago = date('Y-m-d H:i:s', strtotime('-5 minutes'));  

$stmt = $db_con->prepare("
    SELECT COUNT(*) AS id FROM users 
    WHERE last_login_time > :five_min_ago");   

$stmt ->execute([':five_min_ago' => $five_min_ago]);

Also pay attention to your date format string, use capital Y and H . 还请注意您的日期格式字符串,使用大写的YH

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

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