I am trying to produce a PHP file, which wouldn't run whether it's already running. Here's the code I am using:

<?php

class Test {
    private $tmpfile;

    public function action_run() {
        $this->die_if_running();
        $this->run();
    }

    private function die_if_running() {
        $this->tmpfile = @fopen('.refresher2.pid', "w");

        $locked = @flock($this->tmpfile, LOCK_EX|LOCK_NB);
        if (! $locked) {
            @fclose($this->tmpfile);
            die("Running 2");
        }
    }

    private function run() {
        echo "NOT RUNNNING";
        sleep(100);
    }
}

$test = new Test();
$test->action_run();

The issue is, after i run this from console, it really works great. However when I attempt to operate it from browser, most all cases can run concurrently. This really is on Home windows 7, XAMPP, PHP 5.3.2. I suppose OS thinks about the problem it's exactly the same process and therefore the functionality falls. It is possible to mix-platform way to produce a PHP script of the type?

Not necessarily almost anything to promising. You cannot use flock for your such as this.

You could utilize system() to begin another (php) procedure that does the securing for you personally. But disadvantages:

  • You must do interprocess communication. Consider a means how you can tell another program when you should release the lock etc. You should use stdin for messenging und use 3 constants or something like that. Within this situation will still be relatively easy
  • It's harmful to performance since you keep creating processes that is costly.

One other way is always to start another program that runs constantly. You connect with it with a couple way of IPC (most likely only use a tcp funnel since it is mix-platform) and permit the program to handle file acces. That program might be a php script within an endless loop too, however it will most likely be better to code this in Java or any other language which has multithreading support.

One other way is always to leverage existing ressources. Produce a dummy database table for locks, create an entry for that file after which do table-row-securing.

One other way could be to not use files, but a database.

I'd an identical problem not long ago. I desired to possess a counter in which the number came back was unique. I made use of a lock-file and only when this instance could produce the lock-file maybe it was permitted to see the file using the current number.

Rather than counting up possibly you are able to permit the script to operate. The secret would be to let consider using a couple of occasions (like 5) having a small wait/sleep among.

function GetNextNumber()
{
  $lockFile = "lockFile.txt";
  $lfh = @fopen($lockFile, "x");
  if (!$lfh)
  {
    $lockOkay = false;
    $count = 0;
    $countMax = 5;

    // Try ones every second in 5 seconds
    while (!$lockOkay & $count < $countMax) 
    {
      $lfh = @fopen($lockFile, "x");
      if ($lfh)
      {
        $lockOkay = true;
      }
      else
      {
        $count++;
        sleep(1);
      }
    }
  }

  if ($lfh)
  {
    $fh = fopen($myFile, 'r+') or die("Too many users. ");
    flock($fh, LOCK_EX);
    $O_nextNumber = fread($fh, 15);
    $O_nextNumber = $O_nextNumber + 1;
    rewind($fh);
    fwrite($fh, $O_knr);
    flock($fh, LOCK_UN);  
    fclose($fh);

    unlink($lockFile); // Sletter lockfilen
  }

  return $O_nextNumber;
}