0

Is there a way to prevent a code-block or a function within a code from running more than once even if I re-execute (or reload) the PHP file?

I mean, can I restrict someone from executing a php script more than once? I can't seem to find the way to do this.

hakre
  • 193,403
  • 52
  • 435
  • 836
user765368
  • 19,590
  • 27
  • 96
  • 167
  • 1
    You could make the PHP file delete itself; that makes sure nobody can "reload" it. *Guaranteed to run only once.* – Kerrek SB Nov 14 '11 at 02:20
  • if you explained why you wanted this, then you would get a better answer. with out having a user control system, no way will be 100% reliable. –  Nov 14 '11 at 02:32
  • No, I don't just want to delete the file. I want this because I have a php script that inserts some data into a MySQL database and I don't want multiple data to be inserted as many times as the script is executed – user765368 Nov 14 '11 at 02:34
  • 1
    now that you explained a little more, what you should do is check the existence in the db first. –  Nov 14 '11 at 02:35
  • I know I can check in the DB if the value already exists, but can I not do this without checking in the DB? – user765368 Nov 14 '11 at 02:39
  • 1
    check the db with out checking the db? –  Nov 14 '11 at 02:41
  • Sorry for the "great comment" vote, but what I'm saying is that can I not just check if the file has already been executed once without checking in the DB if data has already been inserted? I mean, my script does a LOT of things – user765368 Nov 14 '11 at 02:46
  • how? you could log it, but what's the point, you have your 'check' it's the file in the db. –  Nov 14 '11 at 02:48

4 Answers4

2

Yes, you can use a $_SESSION variable to determine if the code has been executed. The session variable will be set until the user closes their browser. If you want to extend it further than that, you can set a cookie. Please see the following links for more details.

Session Variables

Cookies

Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
  • You can extend the lifetime of the session_id cookie, too. http://stackoverflow.com/questions/8311320/how-to-change-the-session-timeout-in-php – Aloso Mar 25 '17 at 03:11
2

If you are using sessions, then you can set a flag in the user's session array after the code has executed:

function doSomething(){

   if (empty($_SESSION['completed'])){
      //Do stuff here if it has not been executed.
   }

   $_SESSION['completed'] = TRUE;
}

You should also check the sesison variable to see if the task has been executed previously. This assumes that the user can accept a session cookie.

F21
  • 32,163
  • 26
  • 99
  • 170
  • 1
    That doesn't prevent repeated calls on the command line via `$ php thescript.php` from executing the function over and over. The OP's question is just *very* unclear. – Kerrek SB Nov 14 '11 at 02:37
1

Just put a counter in the function. If the counter is greater that 0, then don't do anything. The counter variable should be static so it "remembered" across multiple calls.

function sample() {
     static $call_counter = 0;
     if ( $call_counter>0 ) {
         return;
     }
     ...
     $call_counter++;
 }

As for making sure a file is only executed once, just use "include_once()" instead of "include()".

Brent Baisley
  • 12,641
  • 2
  • 26
  • 39
  • 2
    This will not prevent the code from being executed if the page is reloaded. – Drew Chapin Nov 14 '11 at 02:27
  • Indeed, this won't prevent the code from being executed twice because every time you run the script (reload the php file), your $call_counter will be set to 0 – user765368 Nov 14 '11 at 02:37
  • If declared static, it only gets set the first time the function is called. Subsequent calls remember the last value. Read example #5 http://php.net/manual/en/language.variables.scope.php – Brent Baisley Nov 14 '11 at 11:18
  • The "first time the function is called" _during the same page load_. So in the context of a webpage being reloaded, this is indeed false. The same way it is false in case of a PHP script being executed a second time in a console by the way. – nIcO Dec 14 '11 at 18:52
1

I have an app that does that.

What we did was create a table in the db called version, and stored a version number in there. When the script is ran, it compared the version number in the database with that in the php script. And perform whatever it needs to "upgrade" it to the new version, and then updates the version number in the database.

Of couse, if the version table does not exist, the code will create it and mark it as storing version zero.

iWantSimpleLife
  • 1,944
  • 14
  • 22