=== connection_status() === 测试失败,谁用过,帮我搞搞

hahawen 2006-05-26 04:29:36
<?
set_time_limit(0);
echo str_repeat("a ",300);
ignore_user_abort(false); //this way, the user can stop the output, but not the script.
$i = 0;
while (true) {
echo "test<br>\n";
flush();
sleep(1);
if (connection_status()!=0){
add2file("over");
die(); //kills the script
}
add2file('b');
}
//add2file('b');
function add2file($t){
$fp = fopen("c:/c.txt", 'a+');
fwrite($fp, $t);
fflush($fp);
fclose($fp);
}

?>


无论我把ignore_user_abort(false);设置成true还是false,都不起作用,后台一直运行,根本不像手册中说的那样,我觉得这个函数将来会有用就测试了一下,结果失败!

help!!!!!!!!!!!
...全文
285 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hahawen 2006-05-30
  • 打赏
  • 举报
回复
再顶,谁帮我在php 4.*.*上试验一下,然后留下版本号
xuzuning 2006-05-29
  • 打赏
  • 举报
回复
又做了一下测试,是可以出现
connection_status 3
connection_aborted 1
这样的结果的:虽然点击了“停止”按钮,但程序仍然运行到超时

<?php
function foo() {
$s = "\r\nconnection_status " . connection_status();
$s .= "\r\nconnection_aborted " . connection_aborted();
file_put_contents('__.txt', $s, FILE_APPEND);
}
register_shutdown_function('foo');

file_put_contents('__.txt', '');

ignore_user_abort(true);
set_time_limit(10);

for($i=0; $i<100000000; $i++) {
echo $i;
// if(connection_aborted()) foo();
}
?>
xuzuning 2006-05-29
  • 打赏
  • 举报
回复
好久不见了,都在哪里发财?

你的测试程序的写法好象有问题
一般基本的测试程序应写成这样
<?php
function foo() {
$s = 'connection_status '. connection_status();
file_put_contents('__.txt', $s);
}
register_shutdown_function('foo');
set_time_limit(10);
for($i=0; $i<100000000; $i++)
echo $i;
?>

__.txt文件的内容将是
1、程序正常结束
connection_status 0
2、点击浏览器“停止”按钮
connection_status 1
3、超时
connection_status 2

注意程序一定要有输出,否则ABORTED状态是检测不到的

******

你的程序中有set_time_limit(0);和while (true)
所以程序不会超时和自然结束

而使用connection_status()的地方不对

至于ignore_user_abort函数,因没有测试,不好乱说
mynamesucks 2006-05-29
  • 打赏
  • 举报
回复
失败是指关闭浏览器后c.txt还在写进去b吗
我试了下代码,关了浏览器后c.txt里的b的数量就不会再增加了
hahawen 2006-05-29
  • 打赏
  • 举报
回复
mynamesucks 2006-05-29
  • 打赏
  • 举报
回复
我是5.1.2的
phpini没改过啥,就加了一些常用扩展,还有程序超时时间改的很长
hahawen 2006-05-29
  • 打赏
  • 举报
回复
to: mynamesucks(肖申克的救赎)
你的php是什么版本的?配置文件有没有改过什么特殊的配置?
hahawen 2006-05-29
  • 打赏
  • 举报
回复
to :xuzuning(唠叨)

没发财,我还穷着呢,呵呵。

我的意思不是这样的,看下面的这段话,我是从某一版手册中看到的:
=============================================
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
}
}
?>
================================================
他意思好像应该是说,这个程序运行永不过期,只要用户不关闭页面,页面就在后台一直得跑,用户关闭浏览器的时候,是否停止正在跑得php,是靠ignore_user_abort(true);设置的,但是我我怎么设置它都还在后台跑,怎么设置都不停。我测试的时候是php4.4.*
hahawen 2006-05-28
  • 打赏
  • 举报
回复
丁丁当叮叮当
iasky 2006-05-28
  • 打赏
  • 举报
回复
关注
颓废的老猫 2006-05-26
  • 打赏
  • 举报
回复
不了解,一会去查手册,帮顶

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧