简体   繁体   English

打印 SESSION 中的所有登录用户?

[英]Print all logged-in users in SESSION?

I have a SESSION that supports about 80 users, I want to print all their usernames and refresh the page every 60 seconds or so.我有一个支持大约 80 个用户的 SESSION,我想打印他们的所有用户名并每 60 秒左右刷新一次页面。 I know how to refresh the page but <?php print_r($_SESSION['username']); ?>我知道如何刷新页面,但是<?php print_r($_SESSION['username']); ?> <?php print_r($_SESSION['username']); ?> is only printing the username associated with my personal session. <?php print_r($_SESSION['username']); ?>只打印与我个人 session 关联的用户名。

$_SESSION is unique to each user of the website, it uses cookies. $_SESSION 对网站的每个用户都是唯一的,它使用 cookies。 print_r can only show the contents of the current $_SESSION. print_r 只能显示当前 $_SESSION 的内容。 You would need a database to know all the users' name and Javascript to refresh the page.您需要一个数据库来了解所有用户的名称,并使用 Javascript 来刷新页面。

Unfortunately sessions variables don't work like that.不幸的是会话变量不是那样工作的。

For you to track each session, one method would be to log each login/connection to a MySQL table with a time stamp attached to it.为了让您跟踪每个 session,一种方法是将每个登录/连接记录到 MySQL 表,并附上时间戳。

Then on your tracking page, have it delete the inactive users, then select the rest:然后在您的跟踪页面上,删除非活动用户,然后删除 select 和 rest:

ie IE

mysql_query("DELETE FROM sessions WHERE time < " . time() - 60);

$result = mysql_query("SELECT * FROM sessions");

while ($row = mysql_fetch_assoc($result)) {

    echo $row['username'] . "<br />";
}

Then toss in a <meta http-equiv="refresh" content="60"> to refresh the page ever minute.然后折腾一个<meta http-equiv="refresh" content="60">以每分钟刷新页面。

maybe you want to use something like "memcached" and use a variable there.也许您想使用“memcached”之类的东西并在那里使用变量。

if you store the sessions in a separate folder (and not the default /tmp folder) you could do a list on that folder to find how many sessions are currently set on the server, knowing that it won't be a perfect exact number.如果您将会话存储在单独的文件夹(而不是默认的 /tmp 文件夹)中,您可以在该文件夹上创建一个列表,以查找当前在服务器上设置了多少会话,但知道它不会是一个完美的确切数字。

Database or memcached are probably your best possibiliies.数据库或 memcached 可能是您最好的选择。

Session data is stored in files, depending on your application setup you can parse those files. Session 数据存储在文件中,根据您的应用程序设置,您可以解析这些文件。 Not a very proper solution in my opinion but it will do the trick.在我看来,这不是一个非常合适的解决方案,但它可以解决问题。

You'll need to add a database table of currently logged in users, everytime somebody logs in, add their ID to the table, when they log out (or are timed out) remove the ID, you can save other info besides the ID, such as time, sessionID or IP address or a combination of these.您需要添加当前登录用户的数据库表,每次有人登录时,将他们的 ID 添加到表中,当他们注销(或超时)时删除 ID,您可以保存除 ID 之外的其他信息,例如时间、sessionID 或 IP 地址或这些的组合。 It will give you a fairly good idea of who is online.它可以让您很好地了解谁在线。

To take it one step further, you can have a field that holds the time of the last change of each page, every time you serve that person a page you overwrite this value, this can be used to log out people that just leave your site without doing a logout.更进一步,您可以有一个字段保存每个页面的最后更改时间,每次为该人提供页面时,您都会覆盖此值,这可用于注销刚离开您网站的人无需注销。 For example, if they don't refresh the page for 30 mins you can log that person out of the site.例如,如果他们在 30 分钟内没有刷新页面,您可以将该人从网站中注销。

I would我会

  • Add a 'online' column in the users table.在用户表中添加一个“在线”列。
  • Every time someone logs in or loads a page that they have access to, I would set the online column with the current timestamp.每次有人登录或加载他们有权访问的页面时,我都会使用当前时间戳设置在线列。
  • When checking your online friend list, I'd query everyone who's been online in the pass 60 seconds在查看你的在线好友列表时,我会查询所有在过去 60 秒内在线的人
  • Then make sure your online friend list page updates every 60 seconds.然后确保您的在线好友列表页面每 60 秒更新一次。

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

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