PHP: what happens if required script stops because of timeout - php

Scenario: a php script with som pre-code, a require xxxx.php; and some postcode. Wanting and believing that because of using require and not include, the script will stop at the "require" statement if xxxx.php times out - is that correct?
Or asked in another way: is a timeout the same as the script throwing an error?

From the documentation:
max_execution_time integer
This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.
The maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.
You can not change this setting with ini_set() when running in safe mode. The only workaround is to turn off safe mode or by changing the time limit in the php.ini.
Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.
Official Documentation for set_time_limit()

Related

Can I increase Apache's TimeOut directive for specific PHP script?

I have a PHP script that will be executed by requests from the application admins. It does lots of stuff and takes at least 20 minutes(depending on the database size)
the Apache TimeOut directive is set to 300(5 minutes) which closes the connection and returns 500 status code after my PHP script is finished if it takes longer time to execute
Setting the PHP ini max_execution_time for long time for this script is useless.
<?php
// long script
ini_set("max_execution_time", 3600);// 1 hour
// Apache still responses with the same "Connection: close" header and 500 status code
And I don't want to change the entire Apache TimeOut directive just for those couple of scripts, because if I did, any request will be able to take very long time which makes a scope for DDOS vulnerabilities, is this right?
Is there any way to allow this script only to run longer at the Apache level ?
Have you tried PHP's set_time_limit() method?
https://www.php.net/manual/en/function.set-time-limit.php
In addition to setting the initial execution time, the manual says that calling it resets the time expended to zero when called, and starts the counter again to the limit provided.
So if you want to be sure, you could just call set_time_limit(0) (0 == no limit) regularly throughout your script to make sure that you don't ever hit a limit (even though you're setting an infinite limit by passing in 0).

How do I handle Fatal Error in PHP? [duplicate]

This question already has answers here:
ini_set, set_time_limit, (max_execution_time) - not working
(3 answers)
Closed 8 years ago.
I am inserting a huge data values through CSV file via HTML form.
I have used set_time_limit(0); so that script run until the whole operation is not performed.
Fatal error: Maximum execution time of 300 seconds exceeded in
C:\xampp\htdocs\clytics\include\clytics.database.php on line 135
Now, I am trying to catch this fatal error.
I have used set_time_limit(0); so that script run until the whole
operation is not performed.
Probably needs more memory as well. Basically your code is just a hog & you need to tame the way it is gobbling up resources.
But that is just a tangent on the overall architecture issues you might be facing.
Specific to there issue, is there something in your code that would override that value of set_time_limit(0);?
Also, ar you running this script via the command line or the PHP in Apache? Because the CLI config php.ini is 100% different form the Apache module config of php.ini.
For example, on Ubuntu the Apache PHP php.ini is here:
/etc/php5/apache2/php.ini
But the command line (CLI) php.ini is here:
/etc/php5/cli/php.ini
And if you want to brute force your script to eat up memory regardless of your config settings, you can add this to the top of your PHP file:
ini_set('MAX_EXECUTION_TIME', -1);
If one reads up more on set_time_limit this comes up:
Set the number of seconds a script is allowed to run. If this is
reached, the script returns a fatal error. The default limit is 30
seconds or, if it exists, the max_execution_time value defined in the
php.ini.
Then reading up on max_execution_time this comes up:
This sets the maximum time in seconds a script is allowed to run
before it is terminated by the parser. This helps prevent poorly
written scripts from tying up the server. The default setting is 30.
When running PHP from the command line the default setting is 0.
But then, the magic 300 number shows up:
Your web server can have other timeout configurations that may also
interrupt PHP execution. Apache has a Timeout directive and IIS has a
CGI timeout function. Both default to 300 seconds. See your web server
documentation for specific details.
So now you know where the 300 comes from. But doing ini_set('MAX_EXECUTION_TIME', -1); should let you run the script without a timeout.
And final bit of info if none of that somehow works: Look into max_input_time:
This sets the maximum time in seconds a script is allowed to parse
input data, like POST and GET. Timing begins at the moment PHP is
invoked at the server and ends when execution begins.
While max_input_time might not seem to be related, in some versions of PHP, there is a bug where max_input_time and max_execution_time are directly connected.

PHP Fatal error: Max execution time exceeded - EVEN after set_time_limit(0)

I keep running into the following PHP error when running my script
Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\apps\sqlbuddy1.3.3\functions.php on line 22
I already put this in my PHP file, and I STILL get this error message.
#set_time_limit(0);
Am I missing something?
Edit: This error only shows up after SEVERAL minutes, not after 30 seconds. Could something also be delaying its appearance?
set_time_limit() has no effect when running in safe_mode:
This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.
You can check the value of safe_mode and max_execution_time with phpinfo().
Given the facts that you are using Windows and experiencing the timeout later than 30s you might have somewhere else in your code a reset of the timeout (set_time_limit(30)):
The set_time_limit() function [..] only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.
Search your code for:
ini_set('max_execution_time', 30)
set_time_limit(30)
Rather than relying on the PHP file to change the php.ini settings, you should do it yourself. Find where the php.ini is kept for WAMP, and change/add this line:
max_execution_time = 500;
There is a PHP config that unallows the script to change the time_limit.
You can change your PHP behavior on php.ini file

Apache and or PHP Timeouts - Stumped.

I have a PHP script that when called via a browser it times-out after exactly 60 seconds. I have modified httpd.conf and set the Timeout directive to be 300. I have modified all PHP timeout settings to extend longer than 60 seconds. When I run the script from the command line it will complete. When I execute through browser each time after 60 seconds, POOF, timeout.
I have also checked for the existence of timeout directives in any of the .htaccess files. Nothing there.. I am completely stumped.
I am also forcing set_time_limit(0) within the PHP code.
I've been digging and testing for a week and have exhausted my knowledge. Any help is greatly appreciated.
You need to make sure you are setting a higher timeout limit both in PHP and in Apache.
If you set a high max_execution_time in php.ini your script won't timeout, however, if you are not flushing the output butter of the script's results to the browser on a regular basis, the script might time out on the Apache end due to a network timeout.
In httpd.conf do:
Timeout 216000
In php.ini do:
max_execution_time = 0
(setting it to 0 makes it never time out, like with a CLI (command line) script).
Make sure you restart Apache after you are done! On most linux distro's you can do this by issuing the command (as root):
service httpd restart
Hope this helps!
There are numerous places that the maxtime can be set. If you are using FastCGI, especially though something such as Virtualmin, there are an additional set of settings for max_execution_time that are hidden to you unless you have access.
In short, you will need to figure out all the places, given your PHP stack setup, there can be an execution time limiter, up those values, restart the server, and then do
set_time_limit(0);
for good measure.
Without more information about your specific setup and given my experience in dealing with execution time hangups in PHP, that's the most I can tell you.

PHP command line: max_execution_time and memory_limit

Quick question.
If I run a php script from the command-line (through a cron job, "php ./somwthing-sync.php"), am I still bounded to the php max_execution_time and memory_limit?
Thanks
Nathan
if you look at the doc , its says
max_execution_time integer
This sets the maximum time in seconds a script is allowed to run before it is
terminated by the parser. This helps prevent poorly written scripts from
tying up the server. The default setting is 30.
When running PHP from the command line the default setting is 0.
A 0 means unlimited. (If i am not wrong). As for memory_limit, the PHP command line will take the value that is defined in php.ini. This you can easily test and find out by writing code that "eats" memory and setting memory_limit to -1 in php.ini
I'm guessing you still are, as the page is still executed and takes up memory.
According to ghostdog, the time limit is removed, but my guess is the memory limit is till in place.

Categories