开发者

Progress of heavy php using sessions

开发者 https://www.devze.com 2023-01-27 03:37 出处:网络
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:

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']. Probl开发者_StackOverflow社区em 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.


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.


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 like session_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消