php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72572 Error Reporting on Text-based Web Server
Submitted: 2016-07-10 20:47 UTC Modified: 2016-07-13 10:55 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: David at Refoua dot me Assigned:
Status: Open Package: Unknown/Other Function
PHP Version: 7.0.8 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: David at Refoua dot me
New email:
PHP Version: OS:

 

 [2016-07-10 20:47 UTC] David at Refoua dot me
Description:
------------
When PHP is used with internal error reporting mechanism, PHP will determine if the error message is printed on a command-line interface, or a Web Server (i.e. cgi). All CLI messages are outputted using the text-only format.

However, this only is desired if the Web Server is only returning a HTML file. PHP should determine the current header, and if Content-Type is set to text/plain, or text/*, or anything that a current modern browser would not interpret as a HTML document, any content produced by HTML should be formatted for a text-only output. 

Therefore, PHP's own error reporting should output the same way CLI does, when the header's Content-Type is set to text/plain with PHP.

Test script:
---------------
NOTE: This script is called with cgi from a web server, and not from cli.

<?php
   
   header("Content-Type: text/plain");
   abcd(); // An undefined function to produce error

?>

Expected result:
----------------
Fatal error: Uncaught Error: Call to undefined function abcd() in -:1
Stack trace:
#0 {main}
  thrown in - on line 1

Actual result:
--------------
<br />
<b>Fatal error</b>:  Uncaught Error: Call to undefined function abcd() in -:1
Stack trace:
#0 {main}
  thrown in <b>-</b> on line <b>1</b><br />


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-11 01:14 UTC] cmb@php.net
Good idea, but I'm afraid that is not generally possible. Consider

  <?php
  ob_start();
  trigger_error(…);
  header('Content-Type: …);
  echo ob_get_clean();
 [2016-07-11 11:20 UTC] David at Refoua dot me
You are correct, however in my opinion, that should be considered as bad practice.
PHP should still output the appropriate format, as reasonably possible. Obviously not all types can be covered, but it can handle them much better than current state.

Also, PHP should at least give an option to make whatever it outputs in the text-only or rich HTML format, in order to override what it has determined by itself.
For example, this can make outputting HTML from cli, perhaps by calling a function like: void set_output_format( int PHP_FORMAT_TEXT | PHP_FORMAT_HTML );
Maybe even automatically setting the Content-Type to text/plain instead of the normal html, if this function is called in cgi mode and set_output_format() is set to PHP_FORMAT_TEXT.

This would be a really useful feature for developers in my opinion, especially when writing a quick script in improper environments.
 [2016-07-13 01:33 UTC] kalle@php.net
@cmb, we could also (and I know you'll hate me for saying this), implement it as an ini option to auto detect headers for formatting, thrus disabling it by default to keep BC.
 [2016-07-13 10:55 UTC] David at Refoua dot me
@kalle Why would @cmd hate something like this? I think this would be a great feature for us developers!
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 05:01:24 2019 UTC