I have an application on an Apache2. The applications plays some media files like video files, mp3 files and wav files using a php file in order to avoid direct download from not-registered users. Now, I'm having problems because during the media file is loading, I cannot go to another page on the application until the media file is fully loaded.
I don't know if it's an issue about Apache2 or PHP. Can anybody help me to find out a solution for this issue? It seems that the same client cannot load two instances of PHP pages in the same site.
I'm looking forward your answers. Thanks in advance
If you are using session, I suggest you session_write_close() before you output the file to the browser.
This is because when the session is opened on one page, you cannot load another page until the session has been written and released. session_write_close() is called automatically when your script ends, but because your outputting process takes time before your script end, your session file is locked and thus other pages cannot be viewed.
However, if you are using different browser and/or system, it will be ok because the session file locked is unique to each SESSION ID.
Look at: http://php.net/manual/en/function.session-write-close.php
However do take note that after session_write_close(), you cannot call session_start() or there will be an E_WARNING warning. Also if you make changes to $_SESSION, it will not take effect.
Related
Hoping someone has some ideas around what to do with this.
We have a application thats PHP based hosted in IIS.
There are a number of functions that need to run which can be running for 10mins+. The problem I have is that if I run one of these functions in my web browser. If I open another tab and try to access the site while that is happening then it just sits loading until the long process finishes and then it loads the page.
I guess this is more of a multi session thing to my browser? Is there some easy option in IIS I can change that will let it load the other pages as normal? Or is this a browser thing?
It seems if I open an in private window at the same time, that will load normally.
The issue is related to the session. by default PHP session use a file system.so that has to wait for the session file to be closed before they can open new. Therefore, subsequent requests for the same session wait on prior requests.
To resolve the issue you could try the below things:
-close the session when you are done with it by using the session_write_close()
-Implement a custom DB handler which utilizes the database instead of the file system.
Reference:
FastCGI on IIS7... multiple concurrent requests from same user session?
When I try to upload a file via php copy or ftp put the page is loading and I therefore can not access other pages of the site before the upload completes.
Other pages are loaded after the upload actually is done.
Opening pages in other browsers does work.
Is uploading a file blocking my browser and if yes, why and how do I work around it?
The problem may be related to the locking of session files.
Try to use the session_write_close().
Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.
http://php.net/manual/ru/function.session-write-close.php
I'm using PHP to download a (big) file from a remote server and this download is triggered by clicking on a download button on a web page.
So when i click the download button on the web page, then an Ajax request (with angulars $http) is made to a PHP function. That function triggers a download using cURL.
In the mean time I'd like to make other requests to my PHP site with Ajax. But all other Ajax requests show the status Pending as long as the download is in progress.
So basically the download is blocking every other request to PHP. Is there any way I can avoid this blockage?
This is most likely due to the session file being locked. This is a very common oversight on many php-based web-apps. Essentially, when you call session_start() to access the $_SESSION array, it opens the session file in the tmp directory in read/write mode and locks this file to avoid potential concurrency issues. If you call another script from a different ajax request (or any HTTP request, such as from a new browser window), if that second request also calls session_start, it will wait until the session file is unlocked before moving forward.
The fix is to release the session file once you know you are no longer going to be writing to it. Since your use-case is a huge file download, it is unlikely that during the data output you will need to push anything into the $_SESSION array. You release it from write-mode by calling session_write_close()
I had no idea this was the case until I found out a popular web-app I frequently use was guilty of this. A great blog post on this common bottleneck is:
http://konrness.com/php5/how-to-prevent-blocking-php-requests/
I need to get user download some file (for example, PDF). What will be longer:
send this file by PHP (with specific headers),
or put it in http public folder, and get user the public link to download it (without PHP help)?
In 1st case the original file could be in private zone.
But I'm thinking it will take some time to send this file by PHP.
So how I can measure PHP spent time to sending file and how much memory it can consumed?
P.S. in the 1st case, when PHP sends headers and browser (if pdf plugin is installed) will try to opening it inside browser, is PHP still working, or it push out whole file after headers sent immediately? Or if plugin not installed and browser will show "save as" dialog PHP still working ?
There will be very little in it if you are worried about download speeds.
I guess it comes down to how big your files are, how many downloads you expect to have, and if your documents should be publicly accessible, the download speed of the client.
Your main issue with PHP is the memory it consumes - each link will create a new process, which would be maybe 8M - 20M depending on what your script does, whether you use a framework etc.
Out of interest, I wrote a symfony application to offer downloads, and to do things like concurrency limiting, bandwidth limiting etc. It's here if you're interested in taking a look at the code. (I've not licensed it per se, but I'm happy to make it GPL3 if you like).
i'm having some really strange problem.
i wrote a filemanager in PHP with the ability to download files -- which works fine.
the whole script is built as one big file.
now, while downloading a big file i'm not able to use the script at the same time for, say, browsing folder contents. it does nothing but keep loading. as soon as the download is finished everything works again.
is there something that prevents PHP from parsing the same file concurrently? because other scripts work like a charm, no matter if i'm downloading or not.
help or links to documentation are highly appreciated :)
Do you use sessions?
If yes, then that's probably the problem. The default session handler uses files which have to be locked while session-enabled code is executed. Practically this means that each user executes PHP files sequentially. To solve this you must use a custom session handler that uses a DB. Read this.
Edit: I want to point out that writing a custom session handler with no locking can be difficult and introduce various subtle bugs. Read more docs on this if you need to do it!
Edit 2: Sometimes using session_write_close() to close the session when no longer needed is enough (see the comments).
Daremon is correct, but you shouldn't need to use a different session handler. If you call session_write_close() before you start sending the file, the lock on the session file will be released and your other scripts should be able to continue.