五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > PHP header 的幾種用法和頭部定義

PHP header 的幾種用法和頭部定義

文章來源:365jz.com     點(diǎn)擊數(shù):466    更新時(shí)間:2018-01-09 10:23   參與評(píng)論

header() 函數(shù)向客戶端發(fā)送原始的 HTTP 報(bào)頭。

認(rèn)識(shí)到一點(diǎn)很重要,即必須在任何實(shí)際的輸出被發(fā)送之前調(diào)用 header() 函數(shù)(在 PHP 4 以及更高的版本中,您可以使用輸出緩存來解決此問題):

<?php
header('HTTP/1.1 200 OK'); // ok 正常訪問
header('HTTP/1.1 404 Not Found'); //通知瀏覽器 頁面不存在
header('HTTP/1.1 301 Moved Permanently'); //設(shè)置地址被永久的重定向 301
header('Location: http://www.ithhc.cn/'); //跳轉(zhuǎn)到一個(gè)新的地址
header('Refresh: 10; url=http://www.ithhc.cn/'); //延遲轉(zhuǎn)向 也就是隔幾秒跳轉(zhuǎn)
header('X-Powered-By: PHP/6.0.0'); //修改 X-Powered-By信息
header('Content-language: en'); //文檔語言
header('Content-Length: 1234'); //設(shè)置內(nèi)容長度
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器最后一次修改時(shí)間
header('HTTP/1.1 304 Not Modified'); //告訴瀏覽器文檔內(nèi)容沒有發(fā)生改變
 
###內(nèi)容類型###
header('Content-Type: text/html; charset=utf-8'); //網(wǎng)頁編碼
header('Content-Type: text/plain'); //純文本格式
header('Content-Type: image/jpeg'); //JPG、JPEG 
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音頻文件 
header('Content-type: text/css'); //css文件
header('Content-type: text/javascript'); //js文件
header('Content-type: application/json'); //json
header('Content-type: application/pdf'); //pdf
header('Content-type: text/xml'); //xml
header('Content-Type: application/x-shockw**e-flash'); //Flash動(dòng)畫
 
######
 
###聲明一個(gè)下載的文件###
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="ITblog.zip"');
header('Content-Transfer-Encoding: binary');
readfile('test.zip');
######
 
###對(duì)當(dāng)前文檔禁用緩存###
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
######
 
###顯示一個(gè)需要驗(yàn)證的登陸對(duì)話框### 
header('HTTP/1.1 401 Unauthorized'); 
header('WWW-Authenticate: Basic realm="Top Secret"'); 
######
 
 
###聲明一個(gè)需要下載的xls文件###
header('Content-Disposition: attachment; filename=ithhc.xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: '.filesize('./test.xls')); 
header('Content-Transfer-Encoding: binary'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
readfile('./test.xls'); 
######
?>

PHP header 的幾種用法,你知道幾種?
1. 跳轉(zhuǎn)頁面

header('Location:'.$url);  //Location和":"之間無空格。


2. 聲明content-type

header('content-type:text/html;charset=utf-8');


3. 返回response狀態(tài)碼

header('HTTP/1.1 404 Not Found');


4. 在某個(gè)時(shí)間后執(zhí)行跳轉(zhuǎn)

header('Refresh: 10; url=http://www.baidu.com/');  //10s后跳轉(zhuǎn)。


5. 控制瀏覽器緩存

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");


6. 執(zhí)行http驗(yàn)證

header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');


7. 執(zhí)行下載操作

header('Content-Type: application/octet-stream'); //設(shè)置內(nèi)容類型
header('Content-Disposition: attachment; filename="example.zip"'); //設(shè)置MIME用戶作為附件
header('Content-Transfer-Encoding: binary'); //設(shè)置傳輸方式
header('Content-Length: '.filesize('example.zip')); //設(shè)置內(nèi)容長度


常用用的header狀態(tài)值,記錄下來以免忘記

<?php
//200 正常狀態(tài)
header('HTTP/1.1 200 OK');
 
// 301 永久重定向,記得在后面要加重定向地址 Location:$url
header('HTTP/1.1 301 Moved Permanently');
 
// 重定向,其實(shí)就是302 暫時(shí)重定向
header('Location: http://www.maiyoule.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 錯(cuò)誤
header('HTTP/1.1 404 Not Found');
 
// 500 服務(wù)器錯(cuò)誤
header('HTTP/1.1 500 Internal Server Error');
 
// 3秒后重定向指定地址(也就是刷新到新頁面與 <meta http-equiv="refresh" content="10;http://www.maiyoule.com/ /> 相同)
header('Refresh: 3; url=http://www.maiyoule.com/');
echo '10后跳轉(zhuǎn)到http://www.maiyoule.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è)置頁面最后修改時(shí)間(多用于防緩存)
$time = time() - 60; //建議使用filetime函數(shù)來設(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 的文檔庫,那里很豐富
?>

如對(duì)本文有疑問,請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇

發(fā)表評(píng)論 (466人查看0條評(píng)論)
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)