Home Backend Development PHP Tutorial PHP ignore_user_abort函数详细介绍和使用实例_PHP

PHP ignore_user_abort函数详细介绍和使用实例_PHP

Jun 01, 2016 am 11:51 AM
php

ignore_user_abort 设置与客户机断开是否会终止脚本的执行。

本函数返回 user-abort 设置的之前的值(一个布尔值)。
int ignore_user_abort ([ string $value ] )

 参数setting描述:可选。如果设置为 true,则忽略与用户的断开,如果设置为 false,会导致脚本停止运行。
如果未设置该参数,会返回当前的设置。

注释:PHP 不会检测到用户是否已断开连接,直到尝试向客户机发送信息为止。简单地使用 echo 语句无法确保信息发送,参阅 flush() 函数。

例-1 一个的ignore_user_abort()的例子,配合set_time_limit()函数 和一个死循环就可以实现计划任务功能。
复制代码 代码如下:
ignore_user_abort(true);
 
set_time_limit(0);
echo 'Testing connection handling in PHP';
while (1)
{
    if (connection_status() != CONNECTION_NORMAL) {
 
        break;
 
    }
    // Sleep for 10 seconds
    sleep(10);
}
?>

PHP内置函数研究系列第二期,采用PHP函数ignore_user_abort实现计划任务与持续进程实例,并通过一个可检测效果的实例讨论ignore_user_abort()函数的作用与用法。

ignore_user_abort() 可以实现当客户端关闭后仍然可以执行PHP代码,可保持PHP进程一直在执行,可实现所谓的计划任务功能与持续进程,只需要开启执行脚本,除非 apache等服务器重启或有脚本有输出,该PHP脚本将一直处于执行的状态,初看很实用,不过代价是一个PHP执行脚本的持续进程,开销很大,但却可以 实现很多意想不到的功 能。

其描述为设置与客户机断开是否会终止脚本的执行。

附:另一篇介绍

一,函数原型
复制代码 代码如下:
int ignore_user_abort ( [bool setting] )
二,版本兼容

复制代码 代码如下:PHP 3 >= 3.0.7, PHP 4, PHP 5

三,函数基础用法与实例
1,函数基础用法
复制代码 代码如下:
ignore_user_abort();
?>
说明:调用ignore_user_abort()函数声明即使客户机断开不终止脚本的执行。

2,结合set_time_limit()函数实现一个循环脚本执行任务
复制代码 代码如下:
ignore_user_abort();
set_time_limit(0);
$interval=60*15;
do{
//执行的业务
}while(true);
?>
说明:每隔15分钟循环执行

3,自定义实现文件输出并跟踪ignore_user_abort()函数的执行结果
复制代码 代码如下:
ignore_user_abort ( TRUE );
set_time_limit ( 0 );
$interval = 10;
$stop = 1;
do {
    if( $stop == 10 ) break;
    file_put_contents('liuhui.php',' Current Time: '.time().' Stop: '.$stop);
    $stop++;
    sleep ( $interval );
} while ( true );
?>
打开liuhui.php文件,文件内容如下:
复制代码 代码如下:
Current Time: 1273735029 Stop: 9

其原理是即使客户端终止脚本,仍然每隔10秒钟执行一次,并打印出当前时间与终止点,这样就可以测试出ignore_user_abort()函数的具体效果。

通过实例发现ignore_user_abort()函数非常实用,实现计划任务,完成后续任务,持续进程等非常有效。更多说明请参与PHP手册。请关注下一期PHP内置函数研究系列。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles