Can PHP detect when the server is being shut down?
There is this situation:
- Wampserver is running
- A PHP script is running (set_time_limit(0), endless while loop)
- The server is also used by an user (as, for example gaming PC).
Say the user wants to shutdown the computer, is there any way to detect this in PHP? So the user goes to Start > Shutdown.
If not, is there an easy way to be sure that there will be no开发者_开发技巧 problems with the script? (Like a shutdown between two queries that are both for the same object?)
register_shutdown_function() seems to be the one you are looking for. However, if the OS kills the script, before the shutdown code is completely executed, you cannot do anything from within the script.
If not, is there an easy way to be sure that there will be no problems with the script? (Like a shutdown between two queries that are both for the same object?)
Transactions, but they only ensure, that the data is not corrupted, not that the data is really written.
My advice here: Use a dedicated system.
Windows sends events to applications when a user shuts down their computer. However the user still has the ability to force shutdown and ignore any applications requesting to block the shutdown:
Ability for users to forcefully shut down
In Windows XP, the UI for blocking applications allows users to either cancel shutdown or terminate the blocking application. If subsequent applications also block shutdown, the system displays identical UI for each blocking application. This is frustrating for many users, who, when shutting down, "just want" their computers to turn off.
Windows Vista will solve this by allowing users to terminate the blocking application and make shutdown "forceful." In a forceful shutdown, Windows will send applications WM_QUERYENDSESSION with the ENDSESSION_CRITICAL flag. If an application responds FALSE, Windows will continue shutdown instead of canceling it, and will send the application WM_ENDSESSION. If an application times out responding to WM_QUERYENDSESSION or WM_ENDSESSION, Windows will terminate it.
Source: http://msdn.microsoft.com/en-us/library/ms700677(v=vs.85).aspx
With this in mind you can't rely on being able to block a shutdown even if you listened for these events. Also you won't be able to do anything if they shutdown while trying to do data inserts.
Say the user wants to shutdown the computer, is there any way to detect this in PHP? So the user goes to Start > Shutdown.
The Windows API allows processes to register events that get send to them in case certain actions are performed. These messages are send for cases what you're looking for.
But the application must be able to receive these messages and react upon them in a timely manner. So if your script is busy all the time it can not technically react on to those.
You can use a specialized library like WinBinder to get close to the WINAPI, especially the lowlevel functions the PHP W32api might be another place to look in.
You find Windows Messages documented on many websites and on the microsoft homepage as well (MSDN).
精彩评论