且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP脚本的多个实例不会在同一个浏览器中从同一个URL同时加载

更新时间:2023-11-22 19:37:52

正如VolkerK指出的那样,您的测试证实Firefox / Chrome按顺序对完全相同的 URL执行请求。但实际上这不是问题,因为用户通常不会加载同一页面两次,而对于向Ajax请求提供数据的脚本,由于不同的参数被附加,所以URL会有所不同。



如果你想绕过这个机制,你可以在URL中附加一个随机参数(在不同的浏览器标签中有所不同),即 url.to/your/script?urlID = 4821de7e524cf762deab6ed731343466 。随机参数使浏览器看到两个不同的URL,从而并行执行加载。


I'm trying to get two instances of a php script to run concurrently. I have a script, 'test.php':

<p><?php echo time(); ?> Sleeping...</p>
<?php sleep(5); ?>
<p><?php echo time(); ?> done</p>

If I load the page in two browser tabs at the same time, I get this:

1446855680 Sleeping...

1446855685 done

and this:

1446855686 Sleeping...

1446855691 done

One instance blocks until the other loads. This happens on both Firefox and Chromium.

If I make a second identical script, 'test2.php', or rewrite two urls to the same script, and load the two pages in different tabs, I get this:

1446855862 Sleeping...

1446855867 done

and this:

1446855863 Sleeping...

1446855868 done

Both instances are loading at the same time. So it is identical URLs that are being blocked.

How can I get two instances of a script with the same URL to load/run at the same time?

As VolkerK pointed out and your testing confirmed Firefox/Chrome perform requests for the exact same URL in sequence. However in practice this is not a problem since Users usually do not load the same page twice, and for a script that serves data to Ajax requests the URLs will differ since different parameters are appended.

Should you want to bypass this mechanism in general you can append a random parameter (has to be different in the different browser tabs) to the url, i.e. url.to/your/script?urlID=4821de7e524cf762deab6ed731343466. The random parameter causes the browser to see two different URLs and thus to perform the load in parallel.