404頁面是一種友好的錯誤提示頁面,在php中我們可以利用php,apache,hatcess三種方法來實現向瀏覽器發送404錯誤頁面狀態,下面我來介紹其它的php header函數實現404錯誤頁面的具體方法.
先來了解header函數
header() 函數向客戶端發送原始的 HTTP 報頭,header(string,replace,http_response_code)
php header 404寫法,代碼如下:
header("HTTP/1.1 404 Not Found");exit;
如果以上代碼不湊效,可以試試以下代碼:
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( !emptyempty( $url ) )
- {
- if( !strstr($url ,'Vevb.com' ) && !strstr($url,'Vevb.com'))
- {
- @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狀態才行.
新聞熱點
疑難解答