先來瞭解header函數
header() 函數向用戶端發送原始的 HTTP 前序,header(string,replace,http_response_code)
php header 404寫法:
Php代碼
| 代碼如下 |
複製代碼 |
| header("HTTP/1.1 404 Not Found");exit; |
如果以上代碼不湊效,可以試試以下代碼:
Php代碼
| 代碼如下 |
複製代碼 |
| header("Status: 404 Not Found"); |
上面只是404狀態頁面很難看,如果我們要驗證需要如下操作
| 代碼如下 |
複製代碼 |
@header("http/1.1 404 not found"); @header("status: 404 not found"); include("404.html");//跳轉到某一個頁面,推薦使用這種方法 exit(); |
這樣不但有錯誤提示資訊同時還會有一個漂亮的頁面哦,我在執行個體應用中的一個執行個體
| 代碼如下 |
複製代碼 |
function getref() { $url = @$_SERVER['HTTP_REFERER']; if( !empty( $url ) ) { if( !strstr($url ,'111cn.net' ) && !strstr($url,'111cn.net')) { @header("http/1.1 404 not found"); @header("status: 404 not found"); include("404.html");//跳轉到某一個頁面,推薦使用這種方法 exit(); } } else { @header("http/1.1 404 not found"); @header("status: 404 not found"); include("404.html");//跳轉到某一個頁面,推薦使用這種方法 exit(); } }
|
如果講到apache那就簡單多了,ErrorDocument 404 /404.html
注意了,如果你寫好之後一定要利用專題404錯誤頁面工具進行測試哦,否則可能返回200就不好了,一定要看到是404狀態才行。