实现AJAX状态检查状态、AJAX

2023-09-11 01:06:00 作者:你长得真玩命!

我争夺在PHP 竞争条件保护。

I am battling with race condition protection in PHP.

我的应用程序是用symfony的1.4和PHP锁定会话数据,直到一个页面完成处理。我有一个长期运行的(约10秒)的登录脚本,我想显示一个进度条,显示正在做什么,而他们等待的用户。 (我想实际显示正在做什么,而不是使用标准的[虚假]加载条。)

My application is written in symfony 1.4 and PHP locks session data until a page completes processing. I have a long running (~10 second) login script and I want to display a progress bar showing the user what is being done while they wait. (I want to actually display what is being done and not use a standard [faux] loading bar.)

每当一个脚本调用在session_start(),该用户的会话数据,直到脚本完成PHP锁。这prevents从返回任何东西,直到不再运行脚本完成我的状态检查Ajax调用。 (我为什么我的ajax调用不同步的问题是这里。)

Whenever a script calls session_start(), PHP locks that user's session data until that script completes. This prevents my status check ajax calls from returning anything until the longer running script completes. (My question on why my ajax calls were not asynchronous is here.)

我设计了一个办法做到这一点,但我要确保这种方式是足够安全的一般用途(即 - 这不是一个银行应用程序)。

I have devised a way to do this but I want to make sure this way is secure enough for general purposes (i.e.- this is not a banking application).

我的想法是:

在用户名和放大器的认证;密码(长的登录脚本启动前),一个cookie被设置在客户端计算机与一个唯一的标识符上。 此相同的唯一标识符被写入一个文件的服务器上沿着与客户端的IP地址 而长的登录脚本运行时,它会更新与登录过程的状态该文件。 在阿贾克斯状态检查将ping服务器不使用专门的页面上在session_start()。这个页面将得到cookie的值和客户端的IP和检查服务器端文件的任何状态更新。 On authentication of username & password (before the long login script starts), a cookie is set on the client computer with a unique identifier. This same unique identifier is written to a file on the server along with the client IP address. While the long login script runs, it will update that file with the status of the login process. The ajax status check will ping the server on a special page that does not use session_start(). This page will get the cookie value and the client IP and check the server side file for any status updates.

是否有与此解决方案的任何昭然若揭的问题?的

此外,从安全的角度,即使有人黑了这一切,他们将得到的是一些重presenting的登录进展情况。

Again, from the security angle, even if someone hacked this all they would get is a number representing the state of the login progress.

推荐答案

我看不出有什么内在的错误,你提议的办法。

I don't see anything inherently wrong with the approach that you are proposing.

但是,如果你的机器有 APC 安装,您可以使用 apc_store 和 apc_fetch 来存储在某种共享存储,而不是写入磁盘的状态。使用类似 apc_store(SID,未启动登录')初始化和更新的内存请求的状态,那么 apc_fetch(SID)来检索它在后续的请求。

But if your machine has APC installed you can use apc_store and apc_fetch to store your status in a sort of shared memory instead of writing to disk. Use something like apc_store(SID, 'login not started') to initialize and update the request state in memory, then apc_fetch(SID) to retrieve it on subsequent requests.

有其他的共享存储系统,包括阿帕奇,甚至数据库连接可能比较简单。

There are other shared memory systems, including Apache, or even a database connection might be simpler.