I'm using flock() function to check if another instance of the script is already running by obtaining the lock on a temporary file so next instance should check if the file is not locked otherwise it stops
<?php
$fp = fopen("/var/tmp/your.lock", "w");
if (!flock($fp, LOCK_EX|LOCK_NB)) { // try to get exclusive lock, non-blocking
die("Another instance is running");
}
//my script
sleep(10);
echo 'completed successfully';
the script works without problem when calling the file twice at the same time from different browsers while it waits if I opened two instances at the same time from the same browser i.e the first call get the lock and the second wait for the lock and not closing
I know there may be other ways to check if a file an instance is already working but most of them will do a thing then undo it and in my use case the script may end any time as it may take long time or exceed memory limit or by any reason
any help ?