How can I prevent PHP from executing code multiple times?

I have a WordPress plugin with a backup script that executes on a schedule. The catch is, if someone hits the page multiple times in succession it can execute the backup script multiple times. Any thoughts on how to prevent multiple executions?

global $bwpsoptions;

        if ( get_transient( 'bit51_bwps_backup' ) === false ) {

            set_transient( 'bit51_bwps_backup', '1', 300 );

            if ( $bwpsoptions['backup_enabled'] == 1 ) {

                $nextbackup = $bwpsoptions['backup_next']; //get next schedule
                $lastbackup = $bwpsoptions['backup_last']; //get last backup

                switch ( $bwpsoptions['backup_interval'] ) { //schedule backup at appropriate time
                    case '0':
                        $next = 60 * 60 * $bwpsoptions['backup_time'];
                        break;
                    case '1':
                        $next = 60 * 60 * 24 * $bwpsoptions['backup_time'];
                        break;
                    case '2':
                        $next = 60 * 60 * 24 * 7  * $bwpsoptions['backup_time'];
                        break;
                }

                if ( ( $lastbackup == '' || $nextbackup < time() ) && get_transient( 'bit51_bwps_backup' ) === false ) {

                    $bwpsoptions['backup_last'] = time();

                    if ( $lastbackup == '' ) {

                        $bwpsoptions['backup_next'] = ( time() + $next );

                    } else {

                        $bwpsoptions['backup_next'] = ( $lastbackup + $next );

                    }


                    update_option( $this->primarysettings, $bwpsoptions );

                    $this->execute_backup(); //execute backup

                }

            }

        }

Related posts

Leave a Reply

4 comments

    • Create a file at the start of the code.
    • When the code finishes running delete the file.
    • At the beginning of the code make sure thefile doesn’t exist before running.

    Sort of like the apt-get lock in linux.

  1. If your site is very busy and basic locking mechanism arn’t working (I personally can’t imagine that but oh well!), you can try the solution from PHP session’s garbage collector.

    Just randomly choose a number between 0 and 10 and if the number is 0, do the backup. If now 10 user’s call your backup script at nearly the same time, statistically only one will actually execute the backup.

    define("BACKUP_PROBABILITY", 10);
    if (mt_rand(0, BACKUP_PROBABILITY) == 0)
        doBackup();
    

    You can increase the maximum (the 10) if your site is very highly frequented.

    If in those 10 visits none got the 0, the next 10 visitors will get their chance.

    You will need of course some kind of locking mechanism and it is still possible (though unplausible) that you will end up with more than one or even 10 backups.

    I found this question about mutexes (locks) in PHP. Might be helpful: PHP mutual exclusion (mutex)

  2. I assume that this backup thing makes a backup somewhere.

    So check the metadata on the latest backup, and if it’s creation time is not far enough in the past, don’t do the backup.

    I assume there’s a good reason why this isn’t a cron job?