php ignore_user_abort() 函数设置与客户机断开脚本继续执行
php ignore_user_abort() 函数设置与客户机断开脚本继续执行
ignore_user_abort() 可以实现当客户端关闭后仍然可以执行PHP代码,可保持PHP进程一直在执行,可实现所谓的计划任务功能与持续进程,只需要开启执行脚本,除非 apache 等服务器重启或有脚本有输出,该PHP脚本将一直处于执行的状态,初看很实用,不过代价是一个PHP执行脚本的持续进程,开销很大,但却可以 实现很多意想不到的功能。
定义和用法
ignore_user_abort() 函数设置与客户机断开是否会终止脚本的执行。
本函数返回 user-abort 设置的之前的值(一个布尔值)。
语法
ignore_user_abort(setting)
参数 描述
setting 可选。如果设置为 true,则忽略与用户的断开,如果设置为 false,会导致脚本停止运行。如果未设置该参数,会返回当前的设置。
注释:PHP 不会检测到用户是否已断开连接,直到尝试向客户机发送信息为止。简单地使用 echo 语句无法确保信息发送,参阅 flush() 函数。
使用示例:
(1)结合set_time_limit()函数实现一个循环脚本执行任务
<?php ignore_user_abort(); set_time_limit(0); $interval=60*15;//说明:每隔15分钟循环执行 do{ //执行的业务 }while(true);
(2)自定义实现文件输出并跟踪ignore_user_abort()函数的执行结果
<?php ignore_user_abort(TRUE); set_time_limit(0); $interval=10; $stop=1; do{ if($stop==10) break; file_put_contents('phpernote.com.php',' Current Time: '.time().' Stop: '.$stop); $stop++; sleep($interval); }while(true);
打开 phpernote.com.php 文件,文件内容如下:
Current Time: 1273735029 Stop: 9
其原理是即使客户端终止脚本,仍然每隔10秒钟执行一次,并打印出当前时间与终止点,这样就可以测试出ignore_user_abort()函数的具体效果。
通过实例发现使用 ignore_user_abort()函数在实现计划任务,持续进程等方面的业务时候还是非常实用的。
您可能感兴趣的文章
- thinkphp页面跳转(successerror)如何设置跳转等待时间
- php window平台模拟checkdnsrr函数检测email是否真实存在
- php利用filter函数验证邮箱、url和ip地址的方法
- Jquery等待ajax执行完毕再继续执行下面代码的效果
- php get_headers函数的作用及用法
- 用PHP函数memory_get_usage获取当前PHP内存消耗量以实现程序的性能优化
- php用ZipArchive函数实现文件的压缩与解压缩
- php如何判断当前的操作系统是linux还是windows

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor
