簡體   English   中英

有沒有辦法從PHP獲取當前的HTTP響應代碼?

[英]Is there any way to get the current HTTP response code from PHP?

是否有任何方法可以獲取將從PHP腳本內部發送的HTTP響應代碼,例如,如下所示的腳本:

header('HTTP/1.1 404 Not Found');

$code = imaginary-function(); // $code contains 404

我注意到PHP文檔中的http_response_code函數,但是PHP 5.3.9中似乎沒有該函數。

提前致謝!

它顯然已在PHP 5.4中添加,但我尚未驗證。

從PHP 5.4開始, http_response_code()將獲取或設置該值。

這個headers_list可能就是您想要的

docs:-http: //www.php.net/manual/zh/function.headers-list.php

headers_list()將返回要發送到瀏覽器/客戶端的標題列表。 要確定是否已發送這些標頭,請使用headers_sent()。

如果headers_list沒有滿足您的要求,則這是另一條路線(麻煩):

http://www.php.net/manual/zh/function.header-register-callback.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM