2017-10-10 88 views
0

我在PHP的內置服務器上出現錯誤輸出的奇怪問題,它看起來像一個錯誤,但我不是100%確定。PHP本地服務器 - 雙錯誤 - 錯誤與否?

示例腳本:

<?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); 

如果您在控制檯(php example.php),你會得到以下的,預期的產量執行此代碼:

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 

然而,如果你運行該腳本一個網頁(php -S localhost:9000 example.php),然後打開URL,在控制檯中得到如下輸出:

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 

最奇怪的是,如果在示例腳本的底部添加trigger_error('my error', E_USER_ERROR);,則雙輸出是固定的。

這是什麼原因造成的? INI配置是一個錯誤還是一些怪異?

+2

之前一切確保只有一個HTTP請求。您發佈的輸出對於兩個請求是正常的。 – axiac

+0

你自己試過了嗎? – jurchiks

+0

不知道如果這縮小了它,但我無法在我的Linux機器上覆制它。 –

回答

1

事實上,答案在於這個問題。您使用腳本啓動了內部Web服務器,並將該腳本用作路由器。這意味着它會針對接收到的每個請求調用該腳本。當然,瀏覽器需要favicon.ico以及其他你已經想到的。

啓動了PHP的不帶路由器的內部Web服務器,使用-t命令行選項來告訴它的文件找到服務器(該網站的根目錄下):

php -S localhost:9000 C:\php 
+0

現在誰也不用路由器編寫PHP代碼,cmonman。這種風格如此陳舊,過時並導致不必要的問題。 我只會投票,因爲它會工作,如果我寫這樣的代碼,但我沒有,所以不能接受它。 – jurchiks

+0

現在誰寫了一個沒有'favicon.ico'的網站,cmon man ;-) – axiac

+0

APIs怎麼樣? – jurchiks

0

TL; DR - 沒有真正的解決方案可用,只有解決方法。

龍版本:

顯然,這是由瀏覽器總是請求圖標引起的,並沒有字面上沒有辦法告訴它停下來,不帶頁眉或任何東西。

你能做的唯一的事情就是要麼1)實際上實施的favicon.ico您的API,或2)把一個鏈接到網頁的<head>塊,像這樣:

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

第二個選項明顯將無法用於JSON API等,因此如果您打開的瀏覽器中使用除HTML以外的任何其他響應的路由(例如,從單頁應用程序到後端的API請求),或者甚至直接訪問任何靜態內容(例如http://localhost:9000/css/reset.css,如果還沒有被高速緩存的話,它顯然也會請求一個favicon) - 只有選項1對於那個「好」。