重度php使用session的进度
我正在 php 中进行一些繁重的图像处理,我想向用户展示事情的进展情况。所以我尝试做这样的事情:
for($i = 0; $i < $rowCount; $i++) {
session_start();
$_SESSION['progress'] = $i/ $rowCount;
session_write_close();
processLine($i);
}
然后我使用 AJAX 调用另一个 php 文件,该文件基本上返回 $_SESSION['progress'] 的值。问题是,它没有设置。这是因为您不应该为每个文件多次启动会话吗?如果我只是打开会话并且不在每行之间关闭它,则 SESSION 变量将被锁定,并且在整个图像操作完成之前不会处理我的轮询文件。
有什么办法可以使用会话让它工作吗?我认为如果我将进度放入数据库或文件中可能会起作用,但这很慢,而且我们必须做一些工作来识别用户(使用 ips 或会话 id)。使用会话可以处理这些事情。
I'm doing some heavy image manipulation in php and I would like to show the user how things are progressing. So I tried doing something like this:
for($i = 0; $i < $rowCount; $i++) {
session_start();
$_SESSION['progress'] = $i/ $rowCount;
session_write_close();
processLine($i);
}
And then I'm using AJAX calls to another php file which basically returns the value of $_SESSION['progress']. Problem is, it is not set. Is this because you shouldn't start a session more than once per file? If I just open the session and don't close it between each row, the SESSION variable is locked and my polling file won't be handled until the whole image manipulation is done.
Any way to get this to work using sessions? I think it might work if I put the progress in a database or file, but that's slow and we'll have to do a bit of work to identify users (using ips or session ids). Using sessions would take care of these things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 php 锁定了会话文件。仍然使用相同会话的第一个进程锁定文件,ajax 调用使用相同的会话 ID,因此尝试访问相同的会话文件,但它不能,因为它被 php 锁定,直到第一个进程完成。
这就是你问题的答案。如果你想知道如何解决它,我不确定,我必须考虑一下。
This is because php locks the session file. The first process that still uses the same session locks the file, the ajax call uses the same session ID, thus trying to access the same session file but it can't since it is locked by php until the first process finishes up.
This is the answer to your question. If you want to know how to fix it, I am not sure, I have to think about it.
老实说,您应该在每个使用函数的 PHP 脚本的最顶部放置
session_start();
函数,并且只使用该函数。删除所有其他 session_start 以及您可能拥有的所有其他会话函数,例如session_write_close();
。不要关闭会话或使用其他任何东西,因为这不是必需的。如果该值没有返回,您可能需要仔细检查浏览器中的 JavaScript 控制台,以检查返回的 PHP 错误或文件目录中名为 error_log 的文件。
否则一切都应该按预期进行。我过去做过类似的事情,而且效果很好。我希望这可以帮助您调试问题。
In all honesty, you should just have the
session_start();
function at the very top of each PHP script that uses functions and only that one. Remove all other session_start's and all the other session functions you may have likesession_write_close();
.Do not close sessions or use anything else as it is not required. If the value is not returning you might want to double check the JavaScript console in-browser to check for returned PHP errors or for files named error_log in your file's directory.
Otherwise everything should work just as expected. I have done similar types of things in the past and it worked just fine. I hope this helps you debug your issues.