1

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 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>
user643462
  • 13
  • 2

2 Answers2

3

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;
  • 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.


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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Very clever, I can schedul the archive process during non-peak times as well. – user643462 Mar 03 '11 at 18:38
  • There can be downsides, especially if you get a LOT of deletes in a day and the process overlaps another (can cause the server to bog down in a hurry) That being said, my company uses this process for many different tasks and it's not so bad. And, if you get to the point that you have to scale, you can offload cron tasks to other servers to "work on" – bpeterson76 Mar 03 '11 at 19:42
2

The only way to do this is to include AJAX functionality using a framework (javascript) like jQuery.

The process would be like so:

  1. Click the link to [delete] user
  2. Link is javascript event that fires off to the remote page
  3. While the action is processing a display / loading teaser is showing
  4. The teaser hides/goes away once the deletion is complete
  5. 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:

Community
  • 1
  • 1
Jakub
  • 20,418
  • 8
  • 65
  • 92