简体   繁体   English

防止php脚本同时运行两次

[英]Prevent php script from running simultaneously and twice

I've been looking for a way to prevent running a php script simultaneously. 我一直在寻找一种方法来防止同时运行php脚本。 So I found a way (on this site) to prevent this. 因此,我(在此站点上)找到了一种防止这种情况的方法。 This is where I came with (test file). 这是我附带的(测试文件)。

Link to found solution on stackoverflow: How to prevent PHP script running more than once? 链接到stackoverflow上已找到的解决方案: 如何防止PHP脚本多次运行?

test.php test.php的

echo "started: ".microtime()."<br>";

$lock = $_SERVER['DOCUMENT_ROOT'].'/tmp/test.lock';
$f = fopen($lock, 'x');
if($f === false){
    die("\nCan't aquire lock\n");
}else{
    // Do processing
  echo "Working: ".microtime()."<br>";
    sleep(5);

    echo "Still working: ".microtime()."<br>";
    sleep(5);

    echo "Ready: ".microtime()."<br>";
    fclose($f);
    unlink($lock);      
}

When running this script for the first time, the output will be like this: 首次运行此脚本时,输出将如下所示:

started: 0.87157000 1389879936
Working: 0.87532100 1389879936
Still working: 0.87542000 1389879941
Ready: 0.87551800 1389879946

Now when I run the same script in the same browser simultaneously, both will be executed, however the second one is executed after the first one. 现在,当我在同一浏览器中同时运行同一脚本时,将同时执行这两个脚本,但是第二个脚本将在第一个脚本之后执行。 So not simultaneously, but twice. 因此,不是同时发生,而是两次。 I didn't expect that because it should die if the test.lock file already exists. 我没想到,因为如果test.lock文件已经存在,它应该死了。

So running the script in the same browsers with two tabs, this is the result: 因此,在具有两个选项卡的相同浏览器中运行脚本,结果如​​下:

tab1: TAB1:

started: 0.87157000 1389879936
Working: 0.87532100 1389879936
Still working: 0.87542000 1389879941
Ready: 0.87551800 1389879946

tab2: TAB2:

started: 0.92684500 1389879946
Working: 0.92911700 1389879946
Still working: 0.92920300 1389879951
Ready: 0.92930400 1389879956

As you can see, the script in the 2e tab is started when the script in the first tab is finished. 如您所见,第一个选项卡中的脚本完成后,即会启动2e选项卡中的脚本。 Isn't that weared? 那不是穿吗?

When I do this with different browsers, the script started as second is terminated, so it works. 当我使用不同的浏览器执行此操作时,由于第二个脚本启动的脚本被终止,因此可以正常工作。

browser 1: 浏览器1:

started: 0.62890800 1389880056
Working: 0.63861900 1389880056
Still working: 0.63878800 1389880061
Ready: 0.63893300 1389880066

Browser 2: 浏览器2:

started: 0.10137700 1389880058

Warning: fopen(/home/users/domain/tmp/test.lock) [function.fopen]: failed to open stream: File exists in /home/users/domain/test.php on line 8
Can't aquire lock

The question 问题

I'm now able to prevent executing the script simultaneous, but how to prevent the second script in the same browser of being executed after the first script is finished! 现在,我可以防止同时执行脚本,但是如何防止在第一个脚本完成后在同一浏览器中执行第二个脚本!

I'm guessing that two tabs in the same browser count as the same client, the browser will use the same session. 我猜在同一浏览器中的两个选项卡计为同一客户端,该浏览器将使用相同的会话。 And the server will answer requests from the same session sequentially. 服务器将顺序地回答来自同一会话的请求。 That is why you can be logged into the same service with multiple tabs. 因此,您可以使用多个选项卡登录同一服务。 (Eg have several tabs open in stackoverflow) (例如,在stackoverflow中打开了多个选项卡)

Requests from a different session (browser) may be processed simultaneously. 来自不同会话(浏览器)的请求可以同时处理。 I guess this depends on your server. 我猜这取决于您的服务器。

You can't really prevent the script from being executed twice with a simple lock-file. 您不能真正阻止使用简单的锁定文件将脚本执行两次。 You can only prevent simultaneous execution, as you have demonstrated. 正如您所展示的,您只能阻止同时执行。

If you wanted to prevent the same client from executing a script too often you'd need to keep track of the last time they executed the script. 如果要防止同一客户端过于频繁地执行脚本,则需要跟踪他们上一次执行脚本的时间。 (possibly in a cookie / database) (可能在Cookie /数据库中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM