免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: spiceboy
打印 上一主题 下一主题

忘了,问一下:如何在用户中止了浏览器之后,让php程序继续运行? [复制链接]

论坛徽章:
0
21 [报告]
发表于 2006-04-07 13:13 |只看该作者
carlos at fischerinformatica dot com dot br
31-Jan-2002 10:58
Very very useful!
I was building a chat and I wanted my script to detect when the browser was closed, so the user could be deleted from the online_users table.

<?
echo str_repeat(" ",300);
ignore_user_abort(true); //this way, the user can stop the output, but not the script.
while (true) {
         echo "test<br>\n";
         flush();
         sleep(2);
         if (connection_status()!=0){
                 include ('dbconnect.inc');
                 $sql="delete from online_users where online_user=$user";
                 $sql_exec=pg_exec($vChatDB, $sql);
                 die(); //kills the script
         }
}
?>

论坛徽章:
62
2016科比退役纪念章
日期:2016-06-28 17:45:06奥兰多魔术
日期:2015-05-04 22:47:40菠菜神灯
日期:2015-05-04 22:35:07菠菜神灯
日期:2015-05-04 22:35:02NBA季后赛大富翁
日期:2015-05-04 22:33:34NBA常规赛纪念章
日期:2015-05-04 22:32:032015年亚洲杯纪念徽章
日期:2015-04-14 16:54:452015年亚洲杯之朝鲜
日期:2015-03-19 23:03:16明尼苏达森林狼
日期:2015-03-16 21:51:152015小元宵徽章
日期:2015-03-06 15:57:202015年迎新春徽章
日期:2015-03-04 09:55:282015年辞旧岁徽章
日期:2015-03-03 16:54:15
22 [报告]
发表于 2006-04-07 13:16 |只看该作者
  1. I was building a chat and I wanted my script to detect when the browser was closed
复制代码



如果你也做聊天室,可以考虑。

论坛徽章:
0
23 [报告]
发表于 2006-04-07 13:28 |只看该作者
<superspice AT yeah.net> wrote:

vi ignore.php

  1. <?php
  2. Set_Time_Limit(0);
  3. ignore_user_abort(true);
  4. echo "begin, please close or stop your browser in 10 seconds\n";
  5. sleep(10);
  6. touch("test.txt");
  7. echo "end";
  8. ?>
复制代码


I browsed ignore.php with my browser, 10 seconds later, test.txt was created.
Then I deleted "test.txt",  refreshed this page and closed it within 3 seconds.
After a few seconds, "test.txt" was created as before.

It showed me the case that the aborting of users' browsing had been ignored.

论坛徽章:
0
24 [报告]
发表于 2006-04-07 13:32 |只看该作者
I wrote a simple function that can "spawn" another thread within the webserver by making async http request. The page that is being spawned can call ignore_user_abort() and do whatever it wants in the background...

<?php

   function http_spawn($page)
   {
       $basepath=ereg_replace('[^/]*$', '', $_SERVER['PHP_SELF']);
       $cbSock=fsockopen('localhost', $_SERVER['SERVER_PORT'], $errno, $errstr, 5);
       if ($cbSock)
       {
           fwrite($cbSock, "GET {$basepath}{$page} HTTP/1.0\r\n"
               ."Host: {$_SERVER['HTTP_HOST']}\r\n\r\n");
       }
   }
?>

Example:
<?php

   if ($search_initialized)
       http_spawn("ftindex.php");
?>

论坛徽章:
0
25 [报告]
发表于 2006-04-07 13:41 |只看该作者

  1. <?php
  2. ignore_user_abort(true);
  3. // 1.1 处理用户提交数据
  4. $Adodb->StartTrans();
  5.     .........................
  6.     .........................
  7. $Adodb->CompleteTrans();

  8. /* 用户可能在1.1执行完成后,而2.1没有执行前的此处关闭浏览器或死机? */

  9. // 1.2 处理另一部分数据
  10. $Adodb->StartTrans();
  11.     .........................
  12.     .........................
  13. $Adodb->CompleteTrans();

  14. // 1.3 校验整个数据的一致性和完整性,并写错误日志
  15.    dosomething();
  16. ?>
复制代码


当一个页面里放置了2个或2个以上的事务操作。需要这个页面不被中断。以保持数据的一致性。

论坛徽章:
62
2016科比退役纪念章
日期:2016-06-28 17:45:06奥兰多魔术
日期:2015-05-04 22:47:40菠菜神灯
日期:2015-05-04 22:35:07菠菜神灯
日期:2015-05-04 22:35:02NBA季后赛大富翁
日期:2015-05-04 22:33:34NBA常规赛纪念章
日期:2015-05-04 22:32:032015年亚洲杯纪念徽章
日期:2015-04-14 16:54:452015年亚洲杯之朝鲜
日期:2015-03-19 23:03:16明尼苏达森林狼
日期:2015-03-16 21:51:152015小元宵徽章
日期:2015-03-06 15:57:202015年迎新春徽章
日期:2015-03-04 09:55:282015年辞旧岁徽章
日期:2015-03-03 16:54:15
26 [报告]
发表于 2006-04-07 13:54 |只看该作者
你真好有趣啊, 还是再想想吧

论坛徽章:
0
27 [报告]
发表于 2006-04-08 16:25 |只看该作者
如果用户中止了浏览器之后,php程序还继续运行
服务器能受的了吗

论坛徽章:
0
28 [报告]
发表于 2006-04-08 22:00 |只看该作者
用户所处客户端,php所处服务端

客户端通过浏览器提交请求给服务端,服务端就运行了。

这时和客户端就无关了。即便你关了,只要你的请求合理,服务端当然会工作啦。

请求发出后,客户端只和接受服务端信息有关了。停止浏览器只会影响到返回信息。


楼主是否没有表达准确自己的想法呢?

论坛徽章:
0
29 [报告]
发表于 2006-04-08 22:41 |只看该作者
1. 客户端向服务器端发出请求

2. 服务器端接收并处理请求

3. 服务器端将处理结果返回给客户端

4. 客户端接收服务器端返回的处理结果。

就这么简单,楼主想表达的准确意思是……

论坛徽章:
0
30 [报告]
发表于 2006-04-09 00:34 |只看该作者
支持spiceboy,从你贴的后面的内容里学到了不少东西。

楼上两位没有理解楼主的意思。

b4北京夜狼在里面瞎掺和和讽刺。

楼主,代码这么写,就很清楚明了了:
  1. <?
  2. Set_Time_Limit(0);
  3. echo str_repeat(" ",300);
  4. ignore_user_abort(true);
  5. echo "begin, please close or stop your browser in 10 seconds\n";flush();
  6. sleep(10);
  7. touch("test.txt");
  8. echo "end";
  9. ?>
复制代码


很好玩的个技术。不过正如27楼所说,这个程序得设置好执行条件啊。要不成了死循环的话,服务器就玩完了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP