0

I'm having a weird problem with error output on PHP's built-in server, and it really looks like a bug, but I'm not 100% sure.

Example script:

<?php
error_reporting(-1);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('ignore_repeated_errors', true);

trigger_error('my notice', E_USER_NOTICE);
trigger_error('my warning', E_USER_WARNING);

If you execute this code in the console (php example.php), you get the following, expected, output:

PHP Notice:  my notice in C:\php\example.php on line 7
PHP Stack trace:
PHP   1. {main}() C:\php\example.php:0
PHP   2. trigger_error('my notice', 1024) C:\php\example.php:7
PHP Warning:  my warning in C:\php\example.php on line 8
PHP Stack trace:
PHP   1. {main}() C:\php\example.php:0
PHP   2. trigger_error('my warning', 512) C:\php\example.php:8

However, if you run this script as a webpage (php -S localhost:9000 example.php), and open the URL, you get the following output in the console:

PHP 7.1.10 Development Server started at Tue Oct 10 19:46:05 2017
Listening on http://localhost:9000
Document root is C:\php
Press Ctrl-C to quit.
[Tue Oct 10 19:46:06 2017] PHP Notice:  my notice in C:\php\example.php on line 7
[Tue Oct 10 19:46:06 2017] PHP Stack trace:
[Tue Oct 10 19:46:06 2017] PHP   1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:06 2017] PHP   2. trigger_error('my notice', 1024) C:\php\example.php:7
[Tue Oct 10 19:46:06 2017] PHP Warning:  my warning in C:\php\example.php on line 8
[Tue Oct 10 19:46:06 2017] PHP Stack trace:
[Tue Oct 10 19:46:06 2017] PHP   1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:06 2017] PHP   2. trigger_error('my warning', 512) C:\php\example.php:8
[Tue Oct 10 19:46:07 2017] PHP Notice:  my notice in C:\php\example.php on line 7
[Tue Oct 10 19:46:07 2017] PHP Stack trace:
[Tue Oct 10 19:46:07 2017] PHP   1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:07 2017] PHP   2. trigger_error('my notice', 1024) C:\php\example.php:7
[Tue Oct 10 19:46:07 2017] PHP Warning:  my warning in C:\php\example.php on line 8
[Tue Oct 10 19:46:07 2017] PHP Stack trace:
[Tue Oct 10 19:46:07 2017] PHP   1. {main}() C:\php\example.php:0
[Tue Oct 10 19:46:07 2017] PHP   2. trigger_error('my warning', 512) C:\php\example.php:8

The weirdest thing is that if you add trigger_error('my error', E_USER_ERROR); at the bottom of the example script, the double output is fixed.

What causes this? Is it a bug or some weirdness with the INI configs?

5
  • 2
    Before everything else make sure there is only one HTTP request. The output you posted is normal for two requests. Commented Oct 10, 2017 at 16:58
  • Have you tried it yourself? Commented Oct 10, 2017 at 16:58
  • Dunno if this narrows it down, but I wasn't able to replicate this on my Linux box. Commented Oct 10, 2017 at 17:00
  • 1
    Agree with the two requests idea - the fact the timestamps don't overlap adds to this. Check network tools in the browser/etc. Have you perhaps got a request for a favicon in there? Commented Oct 10, 2017 at 17:00
  • Yeah, just checked network and it requests favicon and it goes to this file as well... What the hell? Commented Oct 10, 2017 at 17:01

2 Answers 2

1

In fact, the answer is in the question. You launched the internal web server with a script and it uses the script as a router. This means it invokes that script for every request it receives. The browser, of course, requires favicon.ico and the rest you already figured out.

Launch the PHP's internal web server without a router, use the -t command line option to tell it where to find the files to server (the root directory of the website):

php -S localhost:9000 C:\php
Sign up to request clarification or add additional context in comments.

3 Comments

Who even writes PHP code without a router these days, cmon man. That style is so old, outdated and causes unnecessary problems. I'll vote it up only because it would work IF I wrote code like that, but I don't, so can't accept it.
Who writes a site without a favicon.ico these days, cmon man ;-)
How about APIs?
0

TL;DR - no real fix available, only workarounds.

Long version:

Apparently this is caused by the browser always requesting a favicon, and there is literally no way to tell it to stop, no headers or anything.

The only thing you can do is either 1) actually implement a favicon.ico for your API, or 2) put a link in your page's <head> block, like so:

<head>
    <link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
</head>

The second option obviously won't work for JSON APIs and such, so if you ever open a route that responds with anything other than HTML in your browser (e.g. an API request from your one-page app to backend), or even access any static content directly (e.g. http://localhost:9000/css/reset.css, which apparently also requests a favicon if one hasn't been cached yet) - only option 1 is "good" for that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.