Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent PHP from executing code multiple times?

Tags:

php

wordpress

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

                }

            }

        }
like image 966
Chris Wiegman Avatar asked May 20 '26 09:05

Chris Wiegman


2 Answers

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

like image 122
mr.user1065741 Avatar answered May 22 '26 23:05

mr.user1065741


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)

like image 38
Basti Avatar answered May 23 '26 00:05

Basti