且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

跟踪用户的在线状态

更新时间:2023-12-02 14:00:10

Mitul写道:
Mitul wrote:

各位大家好,


我正在开发一个社区网站,几乎所有作品都参与其中。

有我面临的主要问题是如何跟踪用户的在线

状态。我正在使用会话数据来保存用户的登录状态。我需要处理2个主要问题才能跟踪用户的在线

状态。


1.当登录用户关闭他/她的浏览器。

2.当会话超时时。


以上两种情况我需要更新数据库中的一个字段,其中包含

用户的在线状态。我真的很困惑。


请帮助我。


欢迎任何帮助或建议。


问候,

Mitul Patel。
Hello everybody,

I am developing a community site and almost all works are competed.
There is major issue that I am facing is how to track user''s online
status. I am using session data to save user''s login status. There are
2 major issues which I need to handle for tracking user''s Online
status.

1. When login user close his/her browser.
2. When session time out.

Both of above case I need to update a field in database which holds
user''s online status. I am really stuck on this.

Please help me.

Any help or suggestion are welcome.

With Regards,
Mitul Patel.



Hi Mitul,

输入基于数据库的sessionstorage的伟大冒险。

它是我知道的***方法。


而不是PHP的默认会话存储(文件中的序列化会话),你可以将
存储在数据库中。


如果您有兴趣,请从这里开始:
http://nl2.php.net/manual/en/functio...ve-handler.php

在构建它时添加一些内务管理(查看垃圾收集

例程)。一旦你有一个数据库,即
持有会话,这一切都非常简单。


但不要指望PHP自动知道用户什么时候关闭窗口。

当发生这种情况时,不会向PHP发送任何事件,因此您将无法使用

默认会话超时来了解用户是否在线。

问候,

Erwin Moller

Hi Mitul,

Enter the great adventure of Database based sessionstorage.
It is the best way I know of.

Instead of PHP''s default sessionstorage (serialized sessions in files), you
can also store them in a DB.

If you are interested, start here:
http://nl2.php.net/manual/en/functio...ve-handler.php
Add some housekeeping to it when you build it (look at garbage collection
routines). It is all very straightforward once you have a database that
holds the sessions.

But don''t expect that PHP automagically knows when a user closes the window.
No event is send to PHP when that happens, so you are stuck with your
default session time-out to know if a user is online.
Regards,
Erwin Moller


谢谢Erwin,


其实我有在数据库中保存了会话ID。但实际上我在浏览器关闭时遇到会话到期的问题
问题。我尝试过在会话存储路径中访问会话文件存储的事情

。如果它在那里
然后我们得出结论,用户仍在那里,如果它不在那里

那么我们就可以说用户的会话已经过期且用户不再

存在。但是有一个问题是这有时会出现故障。


请建议我。


问候,

Mitul Patel。

Thanks Erwin,

Actually I have saved session ID in database. But Actually I am facing
problem with session expire on Browser close. I have tried a things
that I am accessing session file store at session store path. If its
there then we conclude that user is still there and if its not there
then we can say that user''s session is expire and user is no longer
exist. But there is a problem that this is malfunctioning some time.

Please suggest me.

Regards,
Mitul Patel.


Mitul写道:
Mitul wrote:

感谢Erwin,


实际上我已经在数据库中保存了会话ID。但实际上我在浏览器关闭时遇到会话到期的问题
问题。我尝试过在会话存储路径中访问会话文件存储的事情

。如果它在那里
然后我们得出结论,用户仍在那里,如果它不在那里

那么我们就可以说用户的会话已经过期且用户不再

存在。但是有一个问题是这种情况在一段时间内出现故障。


请建议我。
Thanks Erwin,

Actually I have saved session ID in database. But Actually I am facing
problem with session expire on Browser close. I have tried a things
that I am accessing session file store at session store path. If its
there then we conclude that user is still there and if its not there
then we can say that user''s session is expire and user is no longer
exist. But there is a problem that this is malfunctioning some time.

Please suggest me.



Hi Mitul,


看看你的php.ini.

查找关于SESSIONS的部分内容。

查看垃圾收集逻辑/设置。


您找到会话文件的事实并不意味着会话仍然是

有效。

我认为如果

随机数生成器生成小于0.01的数字,PHP将(开箱即用)仅删除旧的会话文件。

因此百分之一。


这是我的(来自旧的php.ini)

; 垃圾收集过程开始的百分比概率

;在每个会话初始化。

session.gc_probability = 1

如果内存为我服务,那么新版本的PHP有两个数字,那个

是分开的。

更好地检查一下你的php.ini所说的内容。


无论如何,让你的directoryscan通过sessionstorage更可靠,

您可能希望将此机会增加到100%,每当有人使用会话时,强制过时的会话将被删除。如果你有很多PHP会话扫描每个

的时间,这当然会减慢你的

应用程序,所以你***考虑一下,或者更好的测试。 :-)


或者你编写自己的sessionstoragehandlers并使用数据库来支持
sessionstorage。我会建议,但如果你之前从未做过/ b $ b,那还有更多的工作要做。基于数据库的会话存储非常快,并且使得它很容易让你看到谁在线。


问候,

Erwin Moller

Hi Mitul,

Have a look at your php.ini.
Find the part about SESSIONS.
Look at the garbagecollection logic/settings.

The fact that you find a sessionfile deos NOT mean that session is still
valid.
I think PHP will (out of the box) only delete old sessionfiles if the
randomnumber generator produces a number smaller than 0.01.
Thus one in a hundred times.

Here is mine (from an old php.ini)
; Percentual probability that the ''garbage collection'' process is started
; on every session initialization.
session.gc_probability = 1

If memory serves me well newer versions of PHP have 2 numbers for this, that
are divided.
Better check carefully by yourself what your php.ini says.

Anyway, to make your directoryscan through the sessionstorage more reliable,
you might want to increase this chance to 100%, forcing stale sessions to
be removed every time anybody uses a session. This will of course slow your
application down a little if you have many sessions for PHP to scan each
time, so you better think this through, or better test. :-)

Or you write your own sessionstoragehandlers and use a database for
sessionstorage. I would advise that, but it is more work if you never did
that before. Database based sessionstorage is very fast, AND makes it
really easy for you to see who is online.

Regards,
Erwin Moller


>

问候,

Mitul Patel。
>
Regards,
Mitul Patel.