本文實例總結(jié)了PHP中Header使用的HTTP協(xié)議及常用方法。分享給大家供大家參考。具體方法如下:
//200 正常狀態(tài)
header('HTTP/1.1 200 OK');
// 301 永久重定向,記得在后面要加重定向地址 Location:$url
header('HTTP/1.1 301 Moved Permanently');
// 重定向,其實就是302 暫時重定向
header('Location: http://www.xxxx.com/');
// 設(shè)置頁面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 服務(wù)器錯誤
header('HTTP/1.1 500 Internal Server Error');
// 3秒后重定向指定地址(也就是刷新到新頁面與 <meta http-equiv="refresh" content="10;http://www.xxxx.com/ /> 相同)
header('Refresh: 3; url=http://www.xxxx.com/');
echo '10后跳轉(zhuǎn)到http://www.xxxx.com';
// 重寫 X-Powered-By 值
header('X-Powered-By: PHP/5.3.0');
header('X-Powered-By: Brain/0.6b');
//設(shè)置上下文語言
header('Content-language: en');
// 設(shè)置頁面最后修改時間(多用于防緩存)
$time = time() - 60; //建議使用filetime函數(shù)來設(shè)置頁面緩存時間
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
// 設(shè)置內(nèi)容長度
header('Content-Length: 39344');
// 設(shè)置頭文件類型,可以用于流文件或者文件下載
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');
//設(shè)置頁面頭信息
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 的文檔庫,那里很豐富
?>
希望本文所述對大家的PHP程序設(shè)計有所幫助。
新聞熱點
疑難解答