search
HomeBackend DevelopmentPHP ProblemHow to change the timeout in php

How to change the timeout in php

Apr 03, 2023 pm 03:14 PM

As our business develops and the number of website visits increases, we often need to execute PHP scripts that take a long time. However, PHP's default timeout is only 30 seconds, which is far from meeting business needs. In order to solve this problem, we need to modify the timeout period of PHP.

Here are some ways to change the PHP timeout:

Modify php.ini

This is the most common way, by modifying the max_execution_time parameter in the php.ini file , you can change PHP's timeout. Find the following content in the php.ini file:

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30

Change the max_execution_time parameter to a value greater than 30 seconds.

It is worth mentioning that if modifying the php.ini file does not work, you need to check which php.ini file is currently used by PHP. This can be viewed in PHP code using the phpinfo() function.

Use set_time_limit() in code

The set_time_limit() function is a built-in function in PHP that can be used to change the maximum time for script execution. For example, add the following code at the beginning of the code:

set_time_limit(600);

The maximum execution time of the PHP script will be set to 600 seconds, which is 10 minutes.

It is worth mentioning that if the parameter is passed in 0, it means that the execution time limit is cancelled, and the script can continue to execute.

Set in Apache

If PHP runs as an Apache module, you can change the PHP timeout by modifying php_value in the .htaccess file.

Add the following code in the .htaccess file:

php_value max_execution_time 600

At this time, the maximum execution time of PHP will be set to 600 seconds, which is 10 minutes.

Set in Nginx or FastCGI

If you are using Nginx or FastCGI, you can add the following code in the configuration file:

fastcgi_read_timeout 600;

At this time, the maximum execution of PHP The time will be set to 600 seconds, which is 10 minutes.

Set in PHP-FPM

If you are using PHP-FPM, you can set the PHP timeout by modifying the php-fpm.conf or www.conf file. Take the php-fpm.conf file as an example here:

Find the following content:

; Process timeouts in seconds. Negative values disable timeouts.
; Default Value:
;     process.timeout = 0s
process.timeout = 30s

Change the process.timeout parameter to a value greater than 30 seconds.

Summary

The above is the method to change the PHP timeout. According to different environments and uses, choose different methods to modify the PHP timeout. In addition, we also need to pay attention to adjusting parameters such as PHP memory limit (memory_limit) and Apache or Nginx server request response time to improve the performance and stability of the website.

The above is the detailed content of How to change the timeout in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

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.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor