科技知識動態:php header 404跳轉錯誤頁面實例

導讀 跟大家講解下有關php header 404跳轉錯誤頁面實例,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說php header 404跳轉錯

跟大家講解下有關php header 404跳轉錯誤頁面實例,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說php header 404跳轉錯誤頁面實例,小編也收集到了有關php header 404跳轉錯誤頁面實例的相關資料,希望大家看到了會喜歡。

404頁面是一種友好的錯誤提示頁面,在php中我們可以利用php,apache,hatcess三種方法來實現向瀏覽器發送404錯誤頁面狀態,下面我來介紹其它的php header函數實現404錯誤頁面的具體方法。

先來了解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狀態才行。

來源:php中文網

免責聲明:本文由用戶上傳,如有侵權請聯系刪除!