浏览器关闭后,PHP能继续执行的函数


希望关闭浏览器后,程序能继续在后台跑,这种情况下需要用到ignore_user_abort()函数;下面是测试代码

<?php

ignore_user_abort(true);    //设置客户端断开连接时是否中断脚本的执行
set_time_limit(0);
$file = './ignore_user.txt';
if(!file_exists($file)) {
	file_put_contents($file);
}
if(!$handle = fopen($file,'a+b')){
	echo "I can not open the file : ".$file;
	exit;
}
$i=0;
while($i<100) {
	$time = date("Y-m-d H:i:s",time());
	echo $time."n";
	if(fwrite($handle, $time."n")===false) {
		echo "I can not write the file : ".$file;
		exit;
	}
	echo "write file time : ".$time."n";
	$i++;
	sleep(1);
}

fclose($handle);

 

参考网址:

浏览器关闭后,php能继续执行的函数 | rin部落


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注