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] )
二,版本兼容
三,函数基础用法与实例
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内置函数研究系列。

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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