在PHP中可以使用header()函數(shù)返回錯(cuò)誤代碼,也就是給頁(yè)面添加404的頭信息,其語(yǔ)法如“header("HTTP/1.0 404 Not Found");”。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php怎么返回錯(cuò)誤代碼?
php中用header()函數(shù)是可以為返回頁(yè)面添加404的頭信息的,從而提示瀏覽器該網(wǎng)頁(yè)找不到了。
所以可以使用:
header("HTTP/1.0 404 Not Found");
或者:
header("Status: 404 Not Found");
后者是在FastCGI模式下使用的,在php代碼中可以把兩句直接同時(shí)寫(xiě)上。
摘抄php手冊(cè)官網(wǎng)的header使用說(shuō)明如下:
The header string.There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.For FastCGI you must use the following for a 404 response:
而header("Location: xxx.com");默認(rèn)是做302狀態(tài)的跳轉(zhuǎn),所以它是不能給瀏覽器輸出404錯(cuò)誤狀態(tài)的。
推薦學(xué)習(xí):《PHP視頻教程》