我有一個 php 代碼,它應該總是以“http 500 代碼”結束,因為我呼叫了 undefined funcstatus_code("OK");
代碼:
<?
# phpinfo();
echo 'error_log = ' . ini_get('error_log') . "\n";
status_code("OK");
echo "OK";
?>
上面的例子:
? curl -I http://localhost:8080/kkk
HTTP/1.1 500 Internal Server Error
Server: nginx/1.20.1
Date: Sat, 29 Oct 2022 18:48:25 GMT
Content-Type: text/html
Content-Length: 5597
Connection: close
ETag: "635c9b20-15dd"
在標準輸出中:
2022/10/29 18:50:32 [error] 29#29: *30 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function status_code() in /legacy/app/index.php:4
Stack trace:
#0 {main}
thrown in /legacy/app/index.php on line 4" while reading response header from upstream, client: 172.17.0.1, server: localhost, request: "HEAD /kkk HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8080"
172.17.0.1 - - [29/Oct/2022:18:50:32 0000] 500 "HEAD /kkk HTTP/1.1" 0 "-" "curl/7.82.0" "-"
但是,如果我取消注釋#phpinfo();行,我會得到 200 OK:
? curl -I http://localhost:8080/kkk
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 29 Oct 2022 18:52:25 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.33
在標準輸出中:
172.17.0.1 - - [29/Oct/2022:18:52:25 0000] 200 "HEAD /kkk HTTP/1.1" 0 "-" "curl/7.82.0" "-"
我正在運行 nginx,它具有以下配置:
location ~ \.php$ {
root /legacy/app;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 10s;
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location / {
# make sure you have a custom html page placed in app folder
# i.e. /legacy/app/app/50x.html
try_files _ /index.php?$query_string;
}
uj5u.com熱心網友回復:
您應該使用內置函式http_response_code()來更改回應代碼。
但是,除了header(),您必須http_response_code()在之前呼叫,phpinfo()因為回應中產生的任何輸出都將暗示帶有 200 狀態代碼的標頭,您無法再更改。(請參閱發送輸出后呼叫 header())
剛剛測驗過Apache(我認為這沒有什么不同)。
這有效(顯示帶有 500 狀態代碼的 php 資訊):
http_response_code(500);
phpinfo();
這不起作用(顯示帶有 200 狀態碼的 php 資訊):
phpinfo();
http_response_code(500);
這不起作用(顯示帶有 200 狀態碼的 php 資訊):
phpinfo();
undefined_function(); // generates an error in error.log
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/525900.html
