session_start如何在PHP中锁定? [英] How does session_start lock in PHP?
问题描述
最初,我只想验证 session_start 是否已锁定会话.因此,我创建了一个PHP文件,如下所示.基本上,如果浏览量是偶数,则页面休眠10秒钟;如果浏览量是奇数,则不是.并且, session_start 用于获取 $ _ SESSION 中的页面视图.
Originally, I just want to verify that session_start locks on session. So, I create a PHP file as below. Basically, if the pageview is even, the page sleeps for 10 seconds; if the pageview is odd, it doesn't. And, session_start is used to obtain the page view in $_SESSION.
我试图在一个浏览器的两个选项卡中访问该页面.自从我明确让它进入睡眠状态以来,第一个标签花费10秒钟就不足为奇了.第二个标签页不会进入睡眠状态,但是应该被 sessiont_start 阻止.符合预期.
I tried to access the page in two tabs of one browser. It is not surprising that the first tab takes 10 seconds since I explicitly let it sleep. The second tab would not sleep, but it should be blocked by sessiont_start. That works as expected.
令我惊讶的是,第二页的输出显示 session_start 几乎不需要时间.实际上,整个页面似乎不需要花费任何时间来加载.但是,该页面确实需要10秒才能在浏览器中显示.
To my surprise, the output of the second page shows that session_start takes almost no time. Actually, the whole page seems takes no time to load. But, the page does take 10 seconds to show in browser.
obtained lock
Cost time: 0.00016689300537109
Start 1269739162.1997
End 1269739162.1998
allover time elpased : 0.00032305717468262
The page views: 101
PHP是否会从PHP页面中提取 session_start 并在其他PHP语句之前执行它?
Does PHP extract session_start out of PHP page and execute it before other PHP statements?
这是代码.
<?php
function float_time()
{
list($usec, $sec) = explode(' ', microtime());
return (float)$sec + (float)$usec;
}
$allover_start_time = float_time();
$start_time = float_time();
session_start();
echo "obtained lock<br/>";
$end_time = float_time();
$elapsed_time = $end_time - $start_time;
echo "Cost time: $elapsed_time <br>";
echo "Start $start_time<br/>";
echo "End $end_time<br/>";
ob_flush();
flush();
if (isset($_SESSION['views']))
{
$_SESSION['views'] += 1;
}
else
{
$_SESSION['views'] = 0;
}
if ($_SESSION['views'] % 2 == 0)
{
echo "sleep 10 seconds<br/>";
sleep(10);
}
$allover_end_time = float_time();
echo "allover time elpased : " . ($allover_end_time - $allover_start_time) . "<br/>";
echo "The page views: " . $_SESSION['views'];
?>
推荐答案
这似乎是与Firefox相关的问题".如果在两个选项卡/窗口中请求相同的url,则第二个请求将等待直到第一个请求完成为止(也可能是阻止第二个请求的插件,尚未进行测试).
以例如
That seems to be a firefox related "issue". If you request the same url in two tabs/windows the second request waits until the first request is finished (could also be an addon that blocks the second request, haven't tested that).
Take e.g.
<?php // test.php
$start = microtime(true);
echo "<pre>start: $start</pre>";
sleep(5);
$end = microtime(true);
echo '<pre>', $start, "\n", $end, "\n", $end-$start, '</pre>';
我叫了两次,输出是
start: 1269742677.6094
1269742677.6094
1269742682.609
4.9995958805084
和
start: 1269742682.6563
1269742682.6563
1269742687.6557
4.9994258880615
请注意,开始时间之间已经有5秒的间隔.
Note that there's already a 5 second gap between the start times.
当两次调用http://localhost/test.php
和http://localhost/test.php?a=b
而不是完全相同的url时,这不会发生.
IE8和Chrome均未显示该行为.
When called as http://localhost/test.php
and http://localhost/test.php?a=b
instead of the exact same url twice this does not happen.
Both IE8 and Chrome do not show that behavior.
这篇关于session_start如何在PHP中锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!