php header 404跳轉錯誤頁面執行個體

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.