PHP:如何在一页上使用会话,而另一页与会话正在加载 [英] PHP: How to use session on one page while another page with session is loading

查看:58
本文介绍了PHP:如何在一页上使用会话,而另一页与会话正在加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CURL从外部URL下载大文件并将其保存在我的服务器上。
这可能需要几分钟。
下载正在运行时,我正在使用curl_setopt($ ch,CURLOPT_PROGRESSFUNCTION,..运行匿名函数,该函数会定期使用当前下载信息更新$ _SESSION ['download_progress']变量。

I am using CURL to download a large file from an external url and save it on my server. This can take up to few minutes. While the download is running I'm using the curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,.. to run anonymous function which periodically updates my $_SESSION['download_progress'] variable with current download info.

现在,所有这一切都发生在upload.php文件中,并且在用户等待文件下载时,我使用javascript请求progress.php页面,其中包含以下简单代码:

Now, all this is happening in upload.php file and while the user is waiting for the file to download I use javascript to request progress.php page which contains the following simple code:

session_start();
echo $_SESSION['download_progress'];

这允许我的javascript代码显示有关下载进度的信息。

This allows my javascript code to display information about the download progress.

除非它不起作用。

progress.php页面在 upload.php完成加载之前不会加载(换句话说,两个页面仅在文件下载完成后加载),并且
session_start()某种程度上阻止了 progress.php页面的加载。
我使用自己的服务器(Apache + PHP 5.4),所以我拥有所有广告最低权利。

The "progress.php" page won't load until the "upload.php" finished loading (in other words both pages load only after the file finished downloading) and this, is not good. The session_start() somehow blocks the "progress.php" page from loading. I use my own server (apache + php 5.4) so I have all admin rights.

如何解决此问题?
我可以使用一些丑陋的解决方法,例如将下载信息写入文本文件而不是会话变量中,然后使用javascript直接读取该文本文件,但是我不想这样做。

How can I solve this problem? I could use some ugly workaround, like writing the download information into a text file instead of session variable and then use the javascript to directly read that text file, but I would rather not do that.

谢谢

推荐答案

文件系统支持的会话存在经典问题。您的上传脚本会在会话备份文件的持续时间内锁定该文件,因此只有在释放锁定后才能访问会话信息。

You have the classic problem with filesystem-backed sessions. Your upload script locks the session backing file for its duration, so it's impossible to get access to the session information until it releases the lock.

这里有几种可能的解决方案,但最简单的方法是让您的上传脚本定期释放会话并再次锁定它;

There are several possible solutions here, but the easiest one would be for your upload script to periodically release the session and lock it again; this would leave opportunity for your progress script to read the session in the meantime.

要释放会话锁,请调用 session_write_close 的任何位置。这使您无法访问会话变量,直到稍后再次调用 session_start 为止。您可以根据需要重复此循环。

To release the session lock, call session_write_close at any point from your upload script. This leaves you without access to session variables until you call session_start again later on. You can repeat this cycle as much as you like.

还有其他功能更强大的解决方案。例如,您可以将进度信息移动到某个存储机制,该存储机制在脚本的持续时间内不会保持锁定状态。您可以根据他们的会话ID来识别每个用户的信息(如果存在会话,则无需启动以获取其ID)。

There are also other, more capable solutions. For example you could move the progress information to some storage mechanism that does not stay locked for the duration of a script; you can identify each user's information based on their session id (you don't need to start the session to get its ID, if one exists).

这篇关于PHP:如何在一页上使用会话,而另一页与会话正在加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆