开发者

PHP session handling when the same client requests the same script multiple times at once

So here's my test setup:

session_start();
if(!isset($_SESSION['bahhhh']))
    $_SESSION['bahhhh'] = 0;
$_SESSION['bahhhh']++;
sleep(5);
die('a'.$_SESSION['bahhhh']);

What I expect to happen is that each time I hit the page, it returns a different number.

But if I use multiple tabs, and refresh them each within 5 seconds of the first, they all return the same number. (This isn't client side caching, as the 5 second delay is still evident.)

Why is it doing this, and how do I fix this?

It seems to have the same strange caching issue with file and database data as well, and is preventing me from building a working mutex to prevent running the same code more than once at开发者_StackOverflow中文版 a time.

Here's another, simpler example:

echo microtime();
sleep(10);

Run this 3 times, each 2 seconds apart, and all three return the same microsecond. WTF?


Session data, be default, is not saved until the request terminates. So your increment is not saved while sleeping. If you want to save the session prematurely checkout session_write_close()


I would have the script itself append something to a log file to verify the script is actually getting executed as many times as you think. Maybe you have some software somewhere that is intercepting the request and returning a cached response.

If it weren't for your comment that this also happens with microtime(), I would have given an explanation of how php manages concurency with sessions, and when it might not.


Apparently this is some bug in my browser itself. Opera behaves this way while Internet Explorer does not. I did initial testing in IE with the same results but with more complex code. Apparently that complex code had an error that triggered the misbehavior in IE, and this simplified code does not. Sorry to bother everyone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜