This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to make PHP set HTTP status code to 500 automatically in case of any error condition? (including those that cannot be handled by user)
I want to configure Apache to send to the user a 500 response with a custom 500 page when a PHP script raises a fatal error.
What I have now is that it will print the error inline with the page content which is good for development but not for production.
How do I configure Apache to send the user a 500 response with a custom page?
Assuming that you don't only want to send a 500 header (which is easy), but really trigger a 500 error in Apache's system, there seems to be no trivial solution.
According to this question, it is PHP's default behaviour since PHP 5.2.4 if:
a fatal error occurs and
the document body is empty (Gordon found the changelog entry here).
I'm not sure how reliable this behaviour is long term (i.e. when PHP 6 comes up etc.). It's not a much advertised feature.
Other than that, I know of no way of provoking a 500 once the script runs. I asked something similar for 404s once. The answer provided there (redirecting to a predefined URL, and sending a 500 header) may be the best you can get - although that of course won't be logged.
I would say that
ErrorDocument 500 /some/page.php
should do the trick, or just a nice HTML page if you want to be sure that it renders properly even when strange things are preventing PHP pages to render.
In order to trigger the error page you look at this anser : How to return an HTTP 500 code on any error, no matter what
I want to do the same thing but I don't think it is possible with the current version of PHP. First, there is a bug where 500 errors return 200 status. So setting the ErrorDocument 500 in your config file will have no effect. In addition, there appears to be no way to access error information (for custom display and logging) even if it could be redirected.
Triggering an error is easy though. Just add this to a PHP file:
throw new Exception();
Related
As a prelude, I know what a HTTP 500 is, and I know how to fix them.
On PHP 5.3, i'm running a production environment with show_errors off. When there are any fatal errors, the user gets a plain white 500 page in response. I'm trying to create a 500 error page just in case there any any errors; just so it is more user friendly.
I used to be able to do
ErrorDocument 500 500.html
It doesn't seem to be working anymore, however - even thought my 404
ErrorDocument 404 404.html
Works fine.
Curious to see solutions regarding this -
Thank you for your time.
Fatal errors don't produce 500 errors in and of themselves, they would return 200 with blank page typically (if no output had been flushed to browser at the point of the error) . Plus this will not help you anyway, as Apache would be no longer involved when PHP is having the error.
Maybe you could register a shutdown function to send 500 header (to get 500 result) and display the content you want to display.
I have a J2.5site set up that uses language. Thus the urls look something like this:
https://www.mysite.com/en/
and
https://www.mysite.com/en/my-component/
and
https://www.mysite.com/en/my-component/my-alias
I have also set up a script that emails me whenever a 404 or 500 etc occurs:
Here is the result of one (of hundreds):
500 - Error: 500
Invalid controller: name='index', format=''
Call stack
Function Location
1 JSite->dispatch() /opt/host/apps/joomla/htdocs/index.php:42
2 JError::raiseError() /opt/host/apps/joomla/htdocs/includes/application.php:208
3 JError::raise() /opt/host/apps/joomla/htdocs/libraries/joomla/error/error.php:251
URL:/en/index.php
Notice the request. If I understand correctly, nothing should ever ask for /en/index.php. since by that time, it has already been interpreted by the entry index.php and therefore appends '/en/'
I am not getting any request errors in my apache logs or any other apache errors.
All that is happening is that I am being bombarded with emails stating that something tried to access http://www.mysite.com/en/index.php or https://www.mysite.com/en/my-component/index.php and thus either generated a 404 or 500 error
Is this spider (search engine bot) issues or is it a server misconfiguration?
Thanks
Jacques
As a prelude, I know what a HTTP 500 is, and I know how to fix them.
On PHP 5.3, i'm running a production environment with show_errors off. When there are any fatal errors, the user gets a plain white 500 page in response. I'm trying to create a 500 error page just in case there any any errors; just so it is more user friendly.
I used to be able to do
ErrorDocument 500 500.html
It doesn't seem to be working anymore, however - even thought my 404
ErrorDocument 404 404.html
Works fine.
Curious to see solutions regarding this -
Thank you for your time.
Fatal errors don't produce 500 errors in and of themselves, they would return 200 with blank page typically (if no output had been flushed to browser at the point of the error) . Plus this will not help you anyway, as Apache would be no longer involved when PHP is having the error.
Maybe you could register a shutdown function to send 500 header (to get 500 result) and display the content you want to display.
I came across an issue today, that my PHP script would send a server error 500 upon finishing (on apache). The code was something like:
//many stuff here that work
echo "It reached here";
exit;
and I was always reaching the point before the exit; command. Doing a google search, I came across this post, which suggested turning on display_errors. I did it and the 500 error went away.
So I wanted to ask, does anyone have an explanation on why this happens? This SO post describes a similar case, but there are no explanations.
As always, thanks in advance
It's pretty likely you get a 500 status code as well, but because PHP echoes something to the browser apache won't jump in and display it's standard status 500 error page.
You would need to verify the actual status code to verify that you won't still get a 500 error.
In case of a fatal error, PHP normally sends a 500 status code. As often the process has failed at that time (hence causing the fatal) and sometime no output is generated since then, the webserver jumps in and gives the user the standard 500-internal-server-error-page.
It appears that the code generated a parse error inside an eval() function in a loop. This kind of error does not break code execution but does produce a 500 error
I am using the Codeigniter framework in a project - I have a tool which reads an array and sends out over 10,000 emails using the SwiftMailer Email framework.
One form which I have once submitted is supposed to send out each individual email, however it doesnt sent out all of them as after a period of time I get the following error:
404 Page Not FoundThe page you requested was not found. - 500.shtml
The page itself doesnt actually redirect anywhere else so cannot understand why it would be saying this - anyone have any ideas?
Thanks
It looks like you're actually ending up with a 500 error, but when CI tries to display the custom error page for a 500 error (500.shtml), it can't find it, and so throws a 404 instead. Check your logs for the cause of the 500 error.
It'll be a custom error page, probably set up on the web server itself. If it's an Apache server, check the httpd config and remove any ErrorDocument directives you don't want so you can see the actual error.
As Tom said, if this is happening after a significant delay, you're likely getting a timeout. The length of timeouts can be increased from the PHP end using set_time_limit() or the php.ini setting max_execution_time. However in general if you have a long-running task it is much better to run it in a background process than try to shoehorn it into an HTTP request.