程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php header功能的使用

php header功能的使用

編輯:關於PHP編程

header() 函數向客戶端發送原始的 HTTP 報頭。
復制代碼 代碼如下:
<?php
//200 正常狀態
header('HTTP/1.1 200 OK');
// 301 永久重定向,記得在後面要加重定向地址 Location:$url
header('HTTP/1.1 301 Moved Permanently');
// 重定向,其實就是302 暫時重定向
header('Location: http://www.maiyoule.com/');
// 設置頁面304 沒有修改
header('HTTP/1.1 304 Not Modified');
// 顯示登錄框,
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="登錄信息"');
echo '顯示的信息!';
// 403 禁止訪問
header('HTTP/1.1 403 Forbidden');
// 404 錯誤
header('HTTP/1.1 404 Not Found');
// 500 服務器錯誤
header('HTTP/1.1 500 Internal Server Error');
// 3秒後重定向指定地址(也就是刷新到新頁面與 <meta http-equiv="refresh" content="10;http://www.jb51.net/ /> 相同)
header('Refresh: 3; url=http://www.jb51.net/');
echo '10後跳轉到http://www.jb51.net';
// 重寫 X-Powered-By 值
header('X-Powered-By: PHP/5.3.0');
header('X-Powered-By: Brain/0.6b');
//設置上下文語言
header('Content-language: en');
 // 設置頁面最後修改時間(多用於防緩存)
$time = time() - 60; //建議使用filetime函數來設置頁面緩存時間
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
// 設置內容長度
header('Content-Length: 39344');
// 設置頭文件類型,可以用於流文件或者文件下載
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
readfile('example.zip');//讀取文件到客戶端

//禁用頁面緩存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Pragma: no-cache');

//設置頁面頭信息
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain');
header('Content-Type: image/jpeg');
header('Content-Type: application/zip');
header('Content-Type: application/pdf');
header('Content-Type: audio/mpeg');
header('Content-Type: application/x-shockwave-flash');
//.... 至於Content-Type 的值 可以去查查 w3c 的文檔庫,那裡很豐富
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved