Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force HTTP status to 500 if any errors occur in PHP file

Tags:

php

First of all Im using PHP 5.4.3.

The problem I have is that whenever an error occurs in a PHP file (such as parse error), it will display an error message in HTML and the HTTP status of the page will be 200.

What I want to know is how can I set the HTTP status to 500 if any errors occur and to display no errors at all.

Keep in mind that I do not want to display HTTP status 500 for every page, only for a few.

like image 539
user937450 Avatar asked Mar 02 '13 22:03

user937450


People also ask

How can I get 500 error in php?

You can solve the PHP error 500 by temporarily deleting the misconfigured htaccess file. The 500 error can go away by increasing the values set for the max_execution_time and the memory_limit settings. Setting the file permission to 644 or 755 can help in resolving the 500 internal server error.

What is HTTP status code 500?

The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request. This error is usually returned by the server when no other error code is suitable.

How do I get PHP error messages?

Quickly Show All PHP Errors The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

Which number class of status codes represents errors?

HTTP status codes of the fourth class represent client errors, i.e. errors that results from a faulty request by the client. A good example in this class is status code 404 Not Found. The fifth class contains server errors. These are errors that are attributed to the server.


2 Answers

You can use register shutdown function. Source: PHP : Custom error handler - handling parse & fatal errors

ini_set('display_errors', 0);

function shutdown() {
    if (!is_null(error_get_last())) {
        http_response_code(500);
    }
}
register_shutdown_function('shutdown');

Note, E_PARSE errors will only be caught in files that are included/required after the shutdown function is registered.

like image 197
mstrthealias Avatar answered Oct 06 '22 00:10

mstrthealias


myninjaname's answer is nearly right - however you don't know what's happened with the output buffer at exit. Also, your code will always run to completion. I'd go with using output buffering and a custom error_handler in addition to handling the situation immediately, guaranteeing the response to the browser this also makes it easier to trap and log information relevant to the error so you can fix any bugs:

<?php

ob_start();

set_error_handler('whoops');

function whoops($errno, $errstr, $errfile, $errline, $errcontext)
{
  header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
  $logged="$errno: $errstr\nin $errfile at $errline\n";
  foreach($errcontext as $scoped=>$val) {
      $logged.=" $scoped = $val\n";
  }
  $logged.="Output = " . ob_get_clean() . "\n";
  // NB still buffering...
  debug_print_backtrace();
  $logged.="stack trace = " . ob_get_clean() . "\n";
  print "whoops";
  ob_end_flush();
  write_log_file($logged);
  exit;
}

You can really handle parse errors - they occur when you've written and deployed bad code - you should know how to prevent this happening on a production system. Adding more code to try to solve the problem is an oxymoron.

You should also disable displaying of errors (not reporting) by setting display_errors=0 in your ini file.

update

I tried to catch any errors that occur with a custom error handler, but for some reason people say that it doesn't catch parse errors (and a few others too).

Don't you know how to test this for yourself? IIRC a lot depends on the context (a shutdown function can catch some parse errors in included content) and the version of PHP. But as above, if you have got to the point where such an error occurs on a production system, it's because you've failed to do your job properly.

like image 30
symcbean Avatar answered Oct 05 '22 22:10

symcbean