English 中文(简体)
会话过期后无法查询
原标题:Can t make query after session expired

I ve binged a lot for this stuff, but couldn t find direct ANSWER, I ve searched for this here,

But I am still beating my head against the wall trying to implement,

How do I do update query after session expired? I m not talking about explicit clicking "Logout" button

基本 SQL 结构 :

CREATE TABLE auth_users (

 email       varchar(40) NOT NULL,
 password    varchar(40) NOT NULL,
 online      ENUM( 1 ) DEFAULT NULL  <-- HERE, it updates to 1 when user logged in, it updates back to NULL when user explicitly clicks on LOGOUT

) type=MyISAM;

Class Hierarchy:

interface {
    function login();
    function logout();
        //calls after succes authorization
        function set_as_online();
        //calls from within logout() method
        function set_as_offline();
}

但它不更新 返回到NULLLL,当用户关闭 他的布朗,

For example, Assume we have two users: User-A, User-B

用户 A 已登录成功, 现在用户B 可以将用户A 视为 ONLINE 用户 。

If User-A forget to click "LOGOUT" and would close his browser, user-B still can see User-A as ONLINE. That s the problem.

For example, Facebook handles this very well,

Assume your friend just closed the browser (i.e his session does not exists anymore), then somehow you can see him as OFFLINE

我做错什么了? 处理离线/在线用户的方法不正确吗?

最佳回答

这里需要另一种方法。 不要设置线上/脱线旗, 而是最后一个时间戳。 每个请求都会将记录更新到当前时间戳。 如果您想知道用户是否在线, 请做 :

if($current_time - $last_seen_time < $session_expire_limit) {
    // online
} else {
    // offline
}

否则,您需要某种coonjob来自动重置数据库中的 online 标志,但您仍然需要一个 last_seen 列。

// 编辑

i don t know exactly how facebook does it, but it could be one of the following; for the chat and notify functionality facebook opens up a stream , which is in fact a little ajax call which is kept alive by the server (btw, this ajax call is refreshed every 40 seconds). Possibly this stream is used to track online users. Another option, but less likely, is that an event is attached to the window.unload event. This is less likely because a page refresh, a clicked link to another facebook page etc. is also triggering the event. This would mean that every time an internal facebook link is clicked the event should be unbinded from the browser. Can t think of another way atm, just some suggestions. Unfortunately those are quite labor-heavy to implement, I assume my suggestion above (before the edit) should be suitable for a common website.

问题回答

我不知道Facebook如何控制这个东西 但我可以建议你 从我的头顶 如何对待这件事情。

我要在您的指定用户表格中添加一个新的字段, 日期将代表会话_ 结束_ 时间 。

Then inside your html pages you should implement some silent ajax code that will call a dummy php page on the server (the interval is something very important because you have to balance performance and functionality). This dummy page will update the session_expiry_time of the user in the auth_users table.

因此,在任何特定时间内,对照当前时间检查用户的会话 _ 过期_ 时间将确定用户是否在线。

https://www.php.net/manual/en/features.connection-handling.php you can with register_shutdown_function() und connection_aborted() your intend achieve

在页面上使用简单的 JavaScript

在身体标签中

调用Logoff () 应替换为 JavaScript 函数, 即调用退出 。

试试看





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签