Table of Contents
register_shutdown_function AND fastcgi_finish_request, registershutdownhook
register_shutdown_function
fastcgi_finish_request
Home Backend Development PHP Tutorial register_shutdown_function AND fastcgi_finish_request,registershutdownhook_PHP教程

register_shutdown_function AND fastcgi_finish_request,registershutdownhook_PHP教程

Jul 12, 2016 am 08:59 AM
and fastcgi function request shutdown

register_shutdown_function AND fastcgi_finish_request, registershutdownhook

In php, the two methods are executed when the request is about to end. The method names are register_shutdown_function and fastcgi_finish_request respectively. Although the timing of execution is similar, the functions and application scenarios are different. Comparing the differences between the two methods is not the focus of this article. The focus of this article is to explain the application scenarios of the two methods.

register_shutdown_function

Function:

Register a method. When a request request is executed, the registered method is called. Note that even if an error occurs during execution and this request is forced to exit, the registered method will still be executed.

Application scenario one:

You can use its features to capture some detailed information of some errors. The sample code is as follows:

<span>function</span><span> catch_error(){
</span><span>$error</span> =<span> error_get_last();
</span><span>if</span>(<span>$error</span><span>){
</span><span>var_dump</span>(<span>$error</span><span>);
}
}
</span><span>register_shutdown_function</span>("catch_error"<span>);
</span><span>ini_set</span>('memory_limit','1M'<span>);
</span><span>$content</span> = <span>str_repeat</span>("aaaaaaaaaaaaaaaaaaaaaaa",100000<span>);
</span><span>echo</span> "aa";
Copy after login

The output information is roughly as follows:

<p>array(4) { ["type"]=> int(1) ["message"]=> string(80) "Allowed memory size of 1048576 bytes exhausted (tried to allocate 2300001 bytes)" ["file"]=> string(39) "/test.php" ["line"]=> int(13) }</p>
Copy after login

It can be seen that the above code captures the out of memory error normally.

Application Scenario 2
Check whether the request is closed normally. The sample code is as follows:

<span>function</span><span> monitor(){
</span><span>global</span> <span>$is_end</span><span>;
</span><span>if</span>(<span>$is_end</span> == <span>true</span><span>){
</span><span>echo</span> "success"<span>;
}</span><span>else</span><span>{
</span><span>echo</span> "fail"<span>;
}
}
</span><span>register_shutdown_function</span>("monitor"<span>);
</span><span>$is_end</span> = <span>false</span><span>;
</span><span>die</span><span>();
</span><span>$is_end</span> = <span>true</span>;
Copy after login

The output result of the page is: fail

It can be seen that even if the die function is called. The registered monitor function also executes normally.

fastcgi_finish_request

Function:
flush data to the client. After calling this method, any output content will not be output to the client.

Application scenarios:

If part of the processing content of a request does not need to be sent to the client, you can first generate the content output to the client, and then call this method. After the method is called, the content will be output to the client. Content that does not need to be output to the client can be placed after this method. This improves responsiveness. The sample code is as follows:

<span>echo</span> "a"<span>;
fastcgi_finish_request();
</span><span>echo</span> "b"<span>;
</span><span>file_put_contents</span>("/tmp/test","abc.com"<span>);
</span><span>die</span><span>();
</span><span>file_put_contents</span>("/tmp/test2","测试数据");
Copy after login

The page output result is: a
It can be seen that the echo "b" after the fastcgi_finish_request method is not output to the client. But you will find that files are created normally in the /tmp/test directory. But the /tmp/bo56 file was not created

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1097792.htmlTechArticleregister_shutdown_function AND fastcgi_finish_request, registershutdownhook In php, the two methods are executed when the request is about to end. The method names are register_shutdown_...
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

Can the linux shutdown command shut down immediately? Can the linux shutdown command shut down immediately? Jan 28, 2023 pm 05:26 PM

Can the linux shutdown command shut down immediately?

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

What does function mean?

3 Ways to Set Up Automatic Shutdown in Windows 10/11 3 Ways to Set Up Automatic Shutdown in Windows 10/11 May 01, 2023 pm 10:40 PM

3 Ways to Set Up Automatic Shutdown in Windows 10/11

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

What does php request mean?

How to set the scheduled shutdown command in Linux How to set the scheduled shutdown command in Linux Feb 18, 2024 pm 11:55 PM

How to set the scheduled shutdown command in Linux

MySQL shutdown unexpectedly - How to solve MySQL error: MySQL shut down unexpectedly MySQL shutdown unexpectedly - How to solve MySQL error: MySQL shut down unexpectedly Oct 05, 2023 pm 02:42 PM

MySQL shutdown unexpectedly - How to solve MySQL error: MySQL shut down unexpectedly

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

What is the Request object in PHP?

How to use the urllib.request.urlopen() function to send a GET request in Python 3.x How to use the urllib.request.urlopen() function to send a GET request in Python 3.x Jul 30, 2023 am 11:28 AM

How to use the urllib.request.urlopen() function to send a GET request in Python 3.x

See all articles