Php's set_time_limit throws a 500 error? - php

When I use set_time_limit and the script runs for any amount of time greater than 360 seconds, it throws a 500 error.
359, nothing, 360 and above, error.
I don't have access to php.ini, how can I fix this bug?

script runs for any amount of time greater than 360 seconds, it throws a 500 error.
It sounds like you're hitting another timeout somewhere. If your server uses FastCGI, for example, Apache and/or the FastCGI process could be configured to only wait for six minutes (360 seconds) before timing out. It also could be that there's a reverse proxy sitting between you and Apache with the same timeout, though proxy timeouts are usually 504s, not 500s.
Please examine your server configuration. If you're on shared hosting, ask your host about the timeout.
If your script needs to execute for an extended time, you may wish to find another way to run it.

If you use Apache you can change maximum execution time by .htaccess with this line
php_value max_execution_time 200

Related

MYSQL The connection has timed out even I have defined in php.ini

I am running some reports and those reports takes more than 300 seconds to execute and finally display on browser.
I have already defined the max execution time in my code and also set the same in php.ini.
ini_set('max_execution_time', 500);
I am using MySQL Workbench to monitor the execution but on 300 sec browser shows
The connection has timed out. The server at localhost is taking too long to
respond.
I just need to extend it to 400-500 sec and all of my reports will start working smoothly.
How can I do that?
Have you tried this:
ini_set('max_execution_time', 0);
This set the maximum execution time to unlimited. You could set this right before calling your report and set it back to a regular value, e.g 300 right after your report is done.
Your web server can have other Timeout configurations settings that may also interrupt PHP execution. Apache has a Timeout Directive which has default value to 300 seconds. Try changing the directive in your conf (httpd.conf for Apache) file.
For more details , See Apache Timeout Directive doc.

cURL loop giving a 500 response

So I have a script which loops doing multiple cURL calls. After about 7-9 minutes it randomly stops execution. I have set the .user.ini file to adjust these settings:
max_execution_time = 30000
max_input_time = 200
I believe I have fastCGI but can't for the life of me figure out why this keeps dying on me. I have a submit form on the front end and I just get a 500 when it dies with nothing in the error log. Anything else I could be missing? Some PHP setting somewhere limiting the number of cURLs or execution time?
EDIT: So this issue was definitely FastCGI limiting my time with the param "FcgidBusyTimeout". My hosting company upped it for me as a test and everything worked great. The issue now is that because I'm on shared hosting they wont up FastCGI timeouts for people. I'm going to try and loop my script onto itself (kind of like a function loop where it calls itself again) and see if the new process' get me past the timeout issue.
FastCGI has its own timeout.
<IfModule mod_fcgid.c>
IPCConnectTimeout 20
IPCCommTimeout 120
FcgidBusyTimeout 200
</IfModule>
So if your PHP timeout is high enough its possible that your FastCGI process were killed after that time.
If you have heavy scripts its better to run the script over your CLI then you have only the PHP Timeout.

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.

exec from php is causing an "Premature end of script headers: php-cgi.exe" error

I have a php script written which calls an external command using exec which compiles a spacial database query result into a shape file. In tables with lots of records (say 15,000), this command can take as long as 7 minutes to execute. The script works fine on scripts which do not take too long (maybe <2min) but on longer scripts (like the 7 minute one) the page will display "500 internal server error". Upon reviewing the Apache server log, the error states: "Premature end of script headers: php-cgi.exe". I have already adjusted the php maximum execution time to allow more than enough time, so I know it is not this. Is there an Apache maximum that's being hit, or something else?
Premature end of script headers means that webserver's timeout for CGI scripts was exceeded by your script. This is a webserver timeout and it has nothing to do with php.ini configuration. You need to look at your CGI handler configuration to increase time allowed for CGI scripts to run.
E.g. if you are using mod_fastcgi you may want to specify the following option in your Apache config: FastCgiServer -idle-timeout 600 which will give you timeout of 10 minutes. By default fastcgi provides 30 seconds. You could find some other fastcgi options here http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html
Apparently, the default httpd.conf file included in Apache 2.4 doesn't automatically include the extra file httpd-default.conf under /conf/extra (probably by design), which includes the timeout parameter. Since the timeout parameter isn't defined, it reverts back to the default value of 30 seconds, and your script(s) time out.
I did the following to resolve it:
Opened httpd.conf and added the following line to the bottom:
Include conf/extra/httpd-default.conf
Restarted Apache, and it worked.
An alternative would be to simply add the following line(s) to the httpd.conf file:
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Hope this helps someone out there :)

How to Extend the Request/Connection Timeout on Apache-FastCGI-PHP application using .htaccess

I am trying to extend the Connection/Request Timeout at our allotted server space.
The Reason i am trying to do this is, for some operations in my application takes more than 120 seconds, then the server is not waiting for the operation to complete. It returns 500 Internal Server Error, exactly after 120 seconds.
To test it i placed the below script on server:
<?php
sleep(119);
echo "TEST";
?>
It will return TEST, to the browser after 119 seconds.
But when i place below script:
<?php
sleep(121);
echo "TEST";
?>
It will return 500 Internal Server Error after 120 seconds
we have set the Max_execution_time=360 in php.ini, but the problem still exists.
We have Apache installed with FastCGI.
I am trying to extend it to 360 seconds, using .htaccess, because that is the only way i can in Shared Hosting.
Any solutions or Suggestions ?, Thanks in Advance.
Fastcgi is a different beast; using set_time_limit will not solve the problem. I'm not sure what you can do with .htaccess, but the normal setting you're looking for is called IPCCommTimeout; you can try to change that in the .htaccess, I'm not sure if it's allowed or not.
See the directives on the apache fcgid page; if you're using an old version, you might need to try setting FcgidIOTimeout instead.
I would suggest that 120 seconds is far too long for a user to wait for a request over a web server; if things take this long to run, try running your script from the command line with PHP CLI instead.
Try this, hope it will work:
set_time_limit(int seconds)

Categories