PHP 中超时或重新激活数据库条目

发布于 2024-12-09 19:57:09 字数 350 浏览 5 评论 0原文

我正在开发一个涉及多个用户和更改数据能力的项目。

基本上,当用户登陆页面时,他可以输入数据库中特定条目的信息。当他在此页面上时,其他用户无法访问该特定条目。当他完成后,入口再次打开。现在,限制对条目的访问很容易。我将其设置为当选择特定条目时,数据库中的一个值表明它处于“非活动”状态,并且其他人无法访问该页面。在页面本身上,有一个“离开”和一个“提交”按钮。其中任何一个都会将条目设置回活动状态。

我遇到的麻烦是,如果用户决定单击不同的链接、关闭选项卡或以某种方式导航离开。我如何构建它以将条目恢复到活动状态?我正在研究“onunload”事件并可能使用它来进行 AJAX 调用。这是最合乎逻辑的路线,还是由于我的知识有限而缺少类似的东西?感谢您的帮助。

I'm working on a project that involves multiple users and the ability to alter data.

Basically, when a user land on a page, he can enter information for a particular entry in a database. While he's on this page, no other user can access that particular entry. When he finishes, the entry becomes open again. Now, to restrict access to the entry is easy. I set it up so when the particular entry is selected, a value in the db states it is "Inactive" and no one else can get to that page. On the page itself, there's a "leave" and a "submit" button. Either of these will set the entry back to active.

The trouble I have is if the user decides to click on a different link, close the tab or somehow navigate away. How can I structure it to restore the entry back to an active state? I was looking into the "onunload" event and potentially using it to make an AJAX call. Is this the most logical route to take or is there something similar I'm missing due to my limited knowledge? Thanks for all your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-12-16 19:57:09

我不会采用 onunload 方式(至少不完全如此),因为它在崩溃、断电等情况下并不可靠。在这种情况下,条目可能会“永远”锁定。

我在页面中嵌入了一个 Ajax 方法,该方法定期请求 PHP 脚本来“确认”该页面仍然存在。

您可以使用此类“确认”请求来构建/更新表来跟踪当前锁定,使用像 lock_id 这样的 s/t 来唯一标识被锁定的“条目”,session_id code> 唯一标识持有该锁的浏览器会话,并 expire_timestamp 设置“条目”应再次解锁的时间,以防不再有“确认”请求session_id 进来并提高其 expire_timestamp

与 cron 作业结合,定期删除超过其 expire_timestamp 的记录,这应该是实现您想要实现的目标的更可靠的方法。

I wouldn't go the onunload way (at least not exclusively), as it's not reliable in case of crash, power loss, etc. Chances are that entries could be locked "forever" in such case.

I'd embed an Ajax method in the page, which periodically requests a PHP script to "confirm" that the page is still alive.

You can use such "confirmation" requests to build/update a table to track current locks, working with s/t like a lock_id which uniquely identifies the "entry" being locked, session_id to uniquely identify the browser session holding that lock and expire_timestamp to set the time at which "entry" should be unlocked again in case no more "confirmation" requests of session_id come in and raise its expire_timestamp.

In combination with a cron job, periodically deleting records having exceeded their expire_timestamp, that should be a more reliably way to do what you are trying to achieve.

烟雨凡馨 2024-12-16 19:57:09

我研究过类似的问题,“onunload”是正确的方法。这样做的缺点是,如果浏览器崩溃或被终止(从任务管理器),则不会调用卸载。因此,如果与该条目对应的 sessionId 不存在,最好有一个将条目设置为活动状态的作业。 (您也可以存储 sessionId 和 isactive 锁的组合来检测浏览器空闲状态)

I worked on a similar problem and "onunload" is the right way to go. The down side of this is that if the browser crashed or is killed (from taskmanager), the unload does not get invoked. So it is better to have a job that sets entries to active state if it is sessionId corresponding to that entry is not present. (you can store a combination of sessionId & isactive lock to detect the browser idleness as well)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文