I have a php script that I want to run. But if it is already running I want any new instances to end immediately.
I have seen other similar questions to this but all the solutions do not work for me. I need it to work when opening two tabs in the same browser and loading both tabs at once. Currently all the solutions I have tried will queue the second page load instead of terminating it.
I have tried the following...
Create a file at the start and unlinking it at the end, and if it exists exit:
<?php
if(file_exists("block.txt"))
{
die('running');
}
else
{
file_put_contents("block.txt", "blocking");
//simulate some work
sleep(60);
echo "END";
unlink("block.txt");
}
?>
I have tried getting a file lock:
<?php
class Lock{
private $fp;
function __construct(){
$this->fp=fopen(__File__,'r');
if (!flock($this->fp,LOCK_EX|LOCK_NB)) {
die('already running !'.PHP_EOL);
}
}
function __destruct(){
flock($this->fp,LOCK_UN);
fclose($this->fp);
}
}
$lock=new Lock();
sleep(60);
echo "END";
?>
and I have also tried making a locking database file
<?php
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
if(!mysqli_query($con, "LOCK TABLES Block WRITE;"))
{
die("blocked");
}
$result = mysqli_query($con, "Select Count(*) as isBlocked from Block;");
if(!$result)
{
mysqli_query($con, "UNLOCK TABLES;");
die("blocked");
}
while($row = mysqli_fetch_assoc($result))
{
$isBlocked = $row['isBlocked'];
}
mysqli_free_result($result);
if($isBlocked > 0)
{
mysqli_query($con, "UNLOCK TABLES;");
die("blocked");
}
else
{
mysqli_query($con, "INSERT INTO Block (x) VALUES (1);");
mysqli_query($con, "UNLOCK TABLES;");
}
sleep(60);
echo "END";
mysqli_query($con, "Truncate Block;");
mysqli_close($con);
?>
When I hit the page in Firefox and then try and open the page in Chrome it works. But if I try and open two tabs in Firefox or two tabs in Chrome it seems to ignore the blocking mechanism and just queues the scripts. How can I make it so that even if I am running it in a separate tab it gets blocked?
All of these examples also have a flaw. If you cancel the page load it never performs the unblocking function and you have to manually force it to unblock. Ideally I would like a solution that does not have this issue.