PHP: Use session on page without interfering with loading session on another page
P粉006977956
P粉006977956 2023-08-08 13:05:15
0
1
520
<p>I am using CURL to download a large file from an external URL and save it on my server. This may take several minutes. During the download, I use curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,.. to run an anonymous function, periodically updating my $_SESSION['download_progress'] variable to get the current download information. </p> <p>Now, all of this happens in the upload.php file, and while the user waits for the file to download, I use JavaScript to request the progress.php page, which contains this simple code: </p> <pre class="brush:php;toolbar:false;">session_start(); echo $_SESSION['download_progress'];</pre> <p>This allows my JavaScript code to display information about the progress of the download. </p> <p>Except it doesn't work. </p> <p>The "progress.php" page won't load until "upload.php" has finished loading (in other words, two pages are loading after the file has finished downloading), which is bad. session_start() somehow prevents the "progress.php" page from loading. I'm using my own server (apache php 5.4) so ​​I have all admin rights. </p> <p>How do I resolve this issue? I could use some unsightly workarounds, such as writing the download information to a text file instead of a session variable, and then using JavaScript to read that text file directly, but I'd rather not do that. Thank you</p><p><br /></p>
P粉006977956
P粉006977956

reply all(1)
P粉638343995

You are encountering a file system-based session problem. The upload script locks the session backend file during execution, so the session information cannot be accessed until the lock is released.

The simplest one is to have your upload script periodically release and relock the session; at the same time, this will provide your progress script with an opportunity to read the session.

To release the session lock, call session_write_close anywhere in the upload script. Doing so will prevent you from accessing the session variables until session_start is called again later. You can repeat this cycle.

There are other more powerful solutions. For example, you could move the progress information to some storage mechanism that doesn't hold a lock during script execution; you could identify each user's information based on the session ID (if a session exists, you don't need to start it to get its ID )

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template