惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - senly

金财微报表的技术支持服务 党建积分的技术支持服务 IE6的position:fixed css秘籍:复选框与文字对齐的问题 php遍历数组 list foreach each方法总结 jQuery cookie 购物车及其实现 CSS自定义滚动条样式 php根据ip查询所在地区(非常有用,赶集网就用到) iframe 父窗口和子窗口相互的调用方法集锦 44种IE css bug实例测试总结 IE6不支持position:fixed的解决方法 jQuery精仿手机上的翻牌效果菜单 jquery菜单左右翻屏效果 jQuery实现无限循环滚动公告 Load JSON data with jQuery, PHP and MySQL php把从数据库读取出来的数据存放到数组里 mysql 实现行号的方法——如何获取当前记录所在行号 DedeCMS会员排行调用代码,实现连接到会员空间 程序员们 不要想一辈子靠技术混饭吃
PHP实现前台同步显示后台任务进度
senly · 2013-07-17 · via 博客园 - senly

一次批量发送几千条短信。 如果直接在后台循环执行虽然可行,但是前台操作用户就只能坐着空等,完全看不到后台执行结果,所以考虑能不能有一种办法可以在php后台执行过程中同时在前台显示后台执行任务进度呢。 但是这里遇到一个问题,一般情况下php都是在后台任务执行完毕后输出结果到浏览器,在执行过程不会给浏览器发送任何数据。这个时候想到了可以使用php的flush函数,可以使用flush函数在程序执行中强制输出; 尝试以下代码:

1

2

3

4

5

6

7

8

<?php

for ($i = 1; $i <= 50; $i++) {

 ob_flush();

 flush();

 echo $i.'<br/>';

 sleep(rand(0, 1));

}

?>

网上大都这样的例子,看代码应该会每隔一秒输出一个数字。但是大家实际测试下情况并非如此,而是和没用flush一样,一次性输出1-50; 在这里请大家参考下鸟哥博客上的一篇文章http://www.laruence.com/2010/04/15/1414.html( 深入理解ob_flush和flush的区别); 其中有一段话:

有些Apache的模块,比如mod_gzip,可能自己进行输出缓存,这将导致flush()函数产生的结果不会立即被发送到客户端浏览器。 

甚至浏览器也会在显示之前,缓存接收到的内容。例如 Netscape浏览器会在接受到换行或 html 标记的开头之前缓存内容,并且在接受到 </table> 标记之前,不会显示出整个表格。 

一些版本的 Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面,所以必须发送一些额外的空格来让这
些浏览器显示页面内容。

所以找到问题所在了,不是因为flush没有起作用,而是服务器或者浏览器在内容不到一定的字节数(甚至是没有遇到html标记)同样会进行缓存,找到问题所在后尝试以下代码;

1

2

3

4

5

6

7

8

9

<?php

echo str_repeat("<b></br>",4096); //随便输出一段代码立即输出

for ($i = 1; $i <= 50; $i++) {

ob_flush();

flush();

echo $i.'<br/>';

sleep(rand(0, 1));

}

?>

已经可以将后台数据实时发送到前台了,然后有办法了吗?结合js,每次循环都输出一段js,去操作html节点;附上disucz中提出出来的安装进度代码供大家参考吧

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<meta content="text/html; charset=utf-8" http-equiv="Content-Type">

<title>Discuz! 安装向导</title>

<style type="text/css">

body{ padding:5px 0; background:#FFF; text-align:center; }

body, td, input, textarea, select, button{ color:#666; font:12px/1.5 Verdana, Tahoma, Arial, 'Microsoft Yahei', 'Simsun', sans-serif; }

.container{ overflow:hidden; margin:0 auto; width:700px; height:auto !important;text-align:left; border:1px solid #B5CFD9; }

.main{ padding:20px 20px 0; background:#F7FBFE url(bg_repx.gif) repeat-x 0 -194px; }

    .main h3{ margin:10px auto; width:75%; color:#6CA1B4; font-weight:700; }

#notice { overflow: hidden; margin: 20px; padding: 5px; height: 300px; border: 1px solid #B5CFD9; text-align: left; }

</style>

<meta name="Copyright" content="Comsenz Inc.">

</head>

<body><div>

<div><script type="text/javascript">

function showmessage(message) {

    document.getElementById('notice').innerHTML += message + '<br/>';

    document.getElementById('notice').scrollTop = 100000000;

}

</script>

        <div id="notice"></div>

<?php

//检测完成后显示的信息

function showjsmessage($message) {

    echo '<script type="text/javascript">showmessage(\''.addslashes($message).' \');</script>'."\r\n";

    flush();

    ob_flush();

}

//模拟初始化数据表

for ($i = 1; $i <= 50; $i++) {

    showjsmessage("建立数据表 {$i} ... 成功");

    sleep(rand(0, 1));

}

?>

</div>

</div>

</body>

</html>