Search code examples
phpsemaphore

PHP semaphore (in same browser)


If I want prevent multiple execution of a code in same browser, why PHP semaphore not working in same browser or another browser tab?

https://www.php.net/manual/en/intro.sem.php

This code work only in different browsers, why not working in same browser, when i execute script simultaneously in same browser (php7.4-fpm + nginx)

$semaphore_key = 25;
$semaphore_max = 1;
$semaphore_permissions = 0666;
$semaphore_autorelease = 1;
$semaphore = sem_get($semaphore_key, $semaphore_max, $semaphore_permissions, $semaphore_autorelease);

if(sem_acquire($semaphore, true) === false) { 
    echo "error";
    exit();
}else{
    echo "ok\n";
}

sleep(30);
sem_release($semaphore);

Solution

  • I don't understand why but it is caused by the Chrome browser cache, the response from the server does not contain any cache headers, when I disable browser cache, it works like expected