同一个浏览器打开多个标签访问同一个网站,只能等待其中一个执行完毕才能执行下一个(php的session锁机制)
2015-01-06 00:00
639 查看
Many people are aware that modern browsers limit the number of concurrent connections to a specific domain to between 4 or 6. This means that if your web page loads dozens of asset files (js, images, css) from the same domain they will be queued up to not exceed this limit. This same problem can happen, but even worse, when your page needs to make several requests to PHP scripts that use sessions.
The other requests will hang on session_start() until the session file is unlocked. This is especially bad if one of your Ajax requests is relatively long-running.
To close the session, call:
view source
print
?
This technique works great if you do not need to write to the session after your long-running process is complete. Fortunately, the $_SESSION data is still available to be read, but since the session is closed you may not write to it.
print
?
Be Sociable, Share!
Problem
PHP writes its session data to a file by default. When a request is made to a PHP script that starts the session (session_start()), this session file is locked. What this means is that if your web page makes numerous requests to PHP scripts, for instance, for loading content via Ajax, each request could be locking the session and preventing the other requests from completing.The other requests will hang on session_start() until the session file is unlocked. This is especially bad if one of your Ajax requests is relatively long-running.
Solution
The session file remains locked until the script completes or the session is manually closed. To prevent multiple PHP requests (that need $_SESSION data) from blocking, you can start the session and then close the session. This will unlock the session file and allow the remaining requests to continue running, even before the initial request has completed.To close the session, call:
view source
?
1.
session_write_close();
This technique works great if you do not need to write to the session after your long-running process is complete. Fortunately, the $_SESSION data is still available to be read, but since the session is closed you may not write to it.
Example
view source?
01.
<?php
02.
// start the session
03.
session_start();
04.
05.
// I can read/write to session
06.
$_SESSION
[
'latestRequestTime'
] =time();
07.
08.
// close the session
09.
session_write_close();
10.
11.
// now do my long-running code.
12.
13.
// still able to read from session, but not write
14.
$twitterId
=
$_SESSION
[
'twitterId'
];
15.
16.
// dang Twitter can be slow, good thing my other Ajax calls
17.
// aren't waiting for this to complete
18.
$twitterFeed
=fetchTwitterFeed(
$twitterId
);
19.
20.
echo
json_encode(
$twitterFeed
);
21.
?>
Be Sociable, Share!
相关文章推荐
- 同一个浏览器打开多个标签访问同一个网站,只能等待其中一个执行完毕才能执行下一个(php的session锁机制)
- 如何让一台电脑只能访问某一个网站?
- PHP的session_set_save_handler 执行机制
- qq邮箱是怎么做到同一个浏览器让多个不用用户同时打开的? --session的控制
- 数据库 'xxx' 已打开,并且一次只能有一个用户访问
- 分享一个检测用户是否用手机(Mobile)访问网站的 PHP 类
- 搞定最为变态的一个Ubuntu下的问题---- 双系统环境下 Ubuntu只能访问百度、谷歌网站!
- php里判断一个网站和网页是否能打开
- qq邮箱是怎么做到同一个浏览器让多个不用用户同时打开的? --session的控制
- 勤于思考:target="_search"解决每次只在浏览器的同一个标签栏中打开
- 今天遇到一个问题:在程序中加入的定时触发器,当时出现这样一个问题,触发器定时2秒,程序从后台查询数据经过复杂处理后时间超过了2秒,我查阅好些网络上说有可能触发器等待程序执行完毕后,在触发下一次,也有说
- 如何设置IE只能访问一个网站
- tomcat部署之后 本机访问本地部署的网站打开一个页面非常慢
- a标签是否新开一个页面的问题各大网站的打开情况整理
- php里判断一个网站和网页是否能打开
- 网站上有一个mp3文件,但是用<a href="my.mp3">下载</a>时,会在播放器里打开,请问我要怎样才能实现点击后下载呢?
- php NetBeans IDE Build 201208070001 打开一个现有的php 网站
- PHP实现点击a标签的href做链接时,直接保存文件(任何类型),而不是通过浏览器直接打开下载的文件
- IE8浏览器访问百度首页,输入关键字,点击“百度一下”,搜索内容出现在一个自动打开的新窗口的问题的一个原因
- 数据库 'xxx' 已打开,并且一次只能有一个用户访问