Jump to content

Php Script Settings


yoded

Recommended Posts

Hi,Main Question: Is it possible to allow a php script wich is user activated to continue untill finished after the user closes or changes the page ?Side Question: Is there a way to delay a function without using the sleep(); function ? (It's the idea that the page loads but that a small part of the page simply waits untill a designated time to execute)I know it's been asked, and I know it's been said, but I do hope someone can give me an answer wich actually helps.

Link to comment
Share on other sites

Most scripts that take a while to run will continue to run if the page is closed. I have a tool for an application I've got, for example, which reorganizes a lot of the database. For installations that have several tens of thousands of users it may take several minutes for the tool to execute. The application uses ajax, so when an admin runs the tool they click the button which sends an ajax request to the server to start running the tool. The ajax request may actually time out, meaning that it returns an error back to the Javascript code, but the script will continue to run on the server until it finishes.

Side Question: Is there a way to delay a function without using the sleep(); function ? (It's the idea that the page loads but that a small part of the page simply waits untill a designated time to execute)
If the PHP code is still running the browser will still display the "loading" message. The browser considers the page finished when the server closes the connection, so as long as the connection is still open and things are still running the page won't have finished loading. Using sleep will only increase the time required for the page to finish loading. If you want code to run after a certain amount of time you need to schedule it to run, on Linux that means using a cron job or Windows has the Scheduled Tasks control panel. You can also use Javascript's setTimeout or setInterval functions to cause a certain piece of Javascript to run after a certain amount of time, and you can use that Javascript to send an ajax request to execute a certain PHP script. If the user closes the page in the meantime then Javascript will not send the ajax request out.You may also be able to fork a new PHP process and sleep for a while before it starts processing things, but I'm not sure how the server and browser will respond if your PHP script has more than one process. The browser may still say "loading" until all processes end, or it may say that it's loading only until the main process finishes while the child process continues to run.What many systems do is have a cron job that executes every minute and checks for work that needs to be done. My application has an email system, for example, so an admin can choose to send an email to maybe 10,000 people at once. But instead of flooding the email server with a bunch of traffic it just queues up the emails in the database and then there's a script that runs every minute to check if there are emails to send which will send a batch of them each minute (500 or 1000 per minute). So that might be something else that would work for your situation.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...