根据 ID 询问 Apache 会话是否仍然存在

发布于 2024-11-09 12:43:02 字数 293 浏览 0 评论 0原文

我想知道如何根据给定的 ID 检查 PHP 会话是否仍然存在。那个或任何可以让我访问活动会话列表的东西。

我在网上找到了一些相关信息,但无法真正得到正确的答案。

为了告诉你一些背景知识,我正在编写一个网站,允许用户修改内容(例如,文章)。由于我不希望他们同时修改相同的资源,因此我必须考虑保护系统。

因此,我们的想法是每个打开的资源都将被锁定并与一个会话 ID 相关联。完成后,用户将释放它。但当然,没有什么可以阻止他关上窗口,让内容锁定在书面上。

因此,我必须能够检查会话是否仍然存在,以及锁是否仍然合法。

I would like to know how to check if an PHP session is still alive, thanks to a given ID. That or anything which could give me access to the active session's list.

I have found some related information on the internet, but couldn't really get a proper answer.

To tell you a bit about the background, I am writing a website which allows users to modify content (let's say articles, for instance). And since I don't want them to modify the same resource at the same time, I had to think about a protection's system.

So the idea was that each opened resource would be locked and associated with a session ID. When finished, the user would free it. But of course nothing prevents him from just closing the window, letting the content locked in writing.

Therefore I have to be able to check if a session is still alive, and if a lock is still legitimate.

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

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

发布评论

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

评论(2

聽兲甴掵 2024-11-16 12:43:02

对于您的问题,有一个非常简单的解决方案:

打开页面时,将用户 ID 和当前时间戳存储在用于锁定元素的某些列中。在页面上,包含一些 JavaScript,以便每分钟调用一些脚本来更新时间戳 - 因此,只要用户在此页面上,时间戳就永远不会早于 1 分钟。只需阻止其他人编辑同一元素,除非时间戳早于 1 分钟(以及几秒)。

There's a pretty simple solution for your problem:

When opening the page, store the userid and the current timestamp in some columns used for locking the element. On the page, include some JavaScript to call some script every minute that updates the timestamp - so as long as the user is on this page the timestamp will never be older than 1 minute. Simply prevent other people from editing the same element unless the timestamp is older than 1 minute (and a few seconds).

远昼 2024-11-16 12:43:02

我知道这可能是一个较旧的问题,但您的问题的“最佳”答案可以在这里找到:
http://www.codeguru.com/forum/archive/index .php/t-372050.html

这是它的内容:
php.ini 文件包含一个名为 sesison.save_path 的设置,该设置确定 PHP 将包含会话数据的文件放置在何处。一旦会话变得陈旧,PHP 将在下一次垃圾收集期间将其删除。因此,对该会话的文件是否存在进行测试应该足以确定该会话是否仍然有效。

$session_id = 'session_id';
$save_path = ini_get('session.save_path');

if (! $save_path) {
$save_path = '.'; // if this value is blank, it defaults to the current directory
}

if (! file_exists($save_path . '/sess_' $session_id)) {
unlink($session_id); // or whatever your file is called
}

I know this might be a older question but the "best" answer to your question is found here:
http://www.codeguru.com/forum/archive/index.php/t-372050.html

Here is what it says:
The php.ini file contains a setting called sesison.save_path, this determines where PHP puts files which contain the session data. Once a session has become stale, it will be deleted by PHP during the next garbage collection. Hence, a test for the presence of a file for that session should be adequate to determine whether the session is still valid.

$session_id = 'session_id';
$save_path = ini_get('session.save_path');

if (! $save_path) {
$save_path = '.'; // if this value is blank, it defaults to the current directory
}

if (! file_exists($save_path . '/sess_' $session_id)) {
unlink($session_id); // or whatever your file is called
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文