如何检测包含我的 PHP 应用程序的最后一个选项卡正在关闭?
我的 PHP 应用程序的菜单上有一个很好的“注销”按钮,但有些人不使用它,他们只是退出浏览器。
有什么方法可以检测到这一点(我意识到它不会处理浏览器崩溃,但它比我现在更好并且涵盖了 99% 的情况)。
这将是我的第一个 JS 作品。
- 我想我可以轻松编写页面关闭处理程序?或者我应该使用onbeforeunload()?
- 在其中,也许我可以遍历所有浏览器窗口及其选项卡(每个浏览器相同或不同?(IE、FF 等))
- 我可以获取每个选项卡的 URL 并检查它是否正在运行我的应用程序(或者是否有更好的方法来做到这一点)?
- 如果这是最后一个选项卡,要么加载一个新的age,即我的应用程序的注销页面,然后关闭它(等待它完全打开?或者只是发送一个HTTP请求并等待200 OK),或者也许一些Ajax?
请更新
没有基于计时器的解决方案。我已经准备好了,但单个用户将超时设置为 999999 分钟,将并发用户数设置为 1,然后关闭他的浏览器并关闭浏览器。更新
。
似乎那些知道的人都说这不能在 JS 中完成,Google 向我建议 DOM 中不支持这一点
但是,还有另一种方式吗?伙计们,横向思考。当用户打开包含我的应用程序页面的新选项卡时,我可以推送 Cookie 吗?然后当用户希望关闭选项卡时删除 cookie?并检查我是否删除了最后一个 cookie &通知服务器将此视为注销?
如果不是cookie还有其他方法吗? (在页面打开和关闭时使用 Ajax,服务器可以决定最后一个选项卡是否已关闭?)
I have a nice Logout button on the menu of my PHP app, but some people don't use it, they just quit the browser.
Is there any way that I can detect this (I realize that it won't handle browser crash, but it's better than I have now & covers 99% of cases).
This will be my first piece of JS.
- I presume that I can easily write a page close handler? Or should I use onbeforeunload()?
- In it, maybe I can walk all browser windows and their tabs (same or different for each browser? (IE, FF, etc))
- I can get the URL of each tab and check if it is running my app (or is there a better way to do it)?
- If this is the last tab, either load a new age which is my app’s logout page, then close it (waiting for it to fully open? Or just send an HTTP request and wait for 200 OK), or maybe some Ajax?
Update
No timer based solutions, please. I have one in place, but a single user set the timeout to 999999 minutes and the number of simultaneous users to 1, then closed his browser & locked himself out
Update
It seems that those here who know say that it can't be done in JS, and Google suggests to me that is isn't supported in the DOM.
But, what about another way? Think laterally, folks. Can I push a cookie when the user opens a new tab with a page from my app? And then remove the cookie when the user wishes to close the tab? And check if I am remvoing the last cookie & inform the server to treat this as logout?
If not cookies is there some other method? (Use Ajax on page open & close and the server can decide if the last tab has closed?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果在过去“x”秒/分钟内没有在服务器端发出请求,则只需使服务器端的会话超时。
通过 JS 执行此操作非常简单,只需每 5 秒向服务器发送一次定期“心跳”请求,该请求会在选项卡关闭时停止。然后,如果您的注销超时为 5 分钟,那么如果 5 分钟内没有收到心跳,您可以尽可能确信客户端已经消失。
例如,如果您的页面包含大量内容(例如一篇长文章),其中用户在几分钟/几小时内不会发出任何请求,但不会关闭选项卡,因此,您希望他们保持活跃的会话。
Just timeout a session on the server side if no requests have been made under it in the last 'x' seconds/minutes.
Doing this via JS could be as simple as sending a periodic "heartbeat" request back to the server once every 5 seconds, which stops when the tab is closed. Then, if your logout timeout is 5 minutes, then if it doesn't get a heartbeat for 5 minutes, you can be as confident as is possible that the client is gone.
This is useful if, for example, you have pages with a ton of content (say a long article) where the user will not be making any requests for several minutes/hours, but doesn't close the tab, and as such, you want them to keep an active session.
onbeforeunload
中添加一些在页面关闭时执行的代码。但是,如果花费的时间太长,浏览器可能会中止您的代码。onbeforeunload
. Browsers may abort your code, though, if it takes too long.没有明确的方法可以完成您所要求的操作,事实上,之前已经在这里询问过:
javascript 检测浏览器关闭选项卡/关闭浏览器
使用会话超时,就像建议的第一条评论一样。
There is no definitive way to do what you're asking, in fact this has been asked before here:
javascript detect browser close tab/close browser
Use a session timeout, like the first comment suggested.