PHP processes before <head>
I have a site for filehosting. In the admin manager, I made a function when a user is deleted, the users uploaded data is compressed, and moved to another server.
The problem is when the admin clicks to delete a user, I would like a message saying "please wait while files are backed up.", but the backup function runs before the header, so you can't write output to the browser until the function is complete. So basicly the admin sees a blank 开发者_Python百科page while the files are being backed up.
The setup is like this:
<?php
if(isset($_POST['submit']))
backupUser()
?>
<html>
<head><title></title></head>
<body>
<backup form>
</body>
</html>
A probably better solution, for that kind of situations, would be :
- When the admin deletes an user, update some field in your users' table, to indicate that that user is currently being deleted
- For instance :
update users set status = 2;
- For instance :
- Then, on a regular basis (once per hour, for example), have a PHP script run in the background (using a cron, typically), that will :
- For each user marked as being deleted :
select * from users where status = 2;
- Really delete the user
- And archive the corresponding files.
- For each user marked as being deleted :
With this :
- The admin can continue working, without having to wait any delay
- Your webserver is not doing heavy operations
You can even have the cron job run only during the night, when the server is not loaded, to avoid degrading its performances during the day, when it's used more.
The only way to do this is to include AJAX functionality using a framework (javascript) like jQuery.
The process would be like so:
- Click the link to [delete] user
- Link is javascript event that fires off to the remote page
- While the action is processing a display / loading teaser is showing
- The teaser hides/goes away once the deletion is complete
- You can reload / redirect / update the page at this point if you need to or just hide the button that was clicked using javascript call.
References:
- jQuery - http://jquery.com/
- How can I create a "Please Wait, Loading..." animation using jQuery?
精彩评论