PHP學(xué)習(xí)筆記-Header請求頭

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/](http://www.ithhc.cn/)'); //跳轉(zhuǎn)到一個(gè)新的地址

header('Refresh: 10; url=[http://www.ithhc.cn/](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動畫

聲明一個(gè)下載的文件

header('Content-Type: application/octet-stream'); //聲明輸出的是二進(jìn)制字節(jié)流

header('Accept-Ranges:bytes');//聲明瀏覽器返回大小是按字節(jié)進(jìn)行計(jì)算

header('Content-Disposition: attachment; filename="ITblog.zip"');

// 聲明作為附件處理和下載后文件的名稱//告訴瀏覽器文件的總大小

//告訴瀏覽器文件的總大小    $fileSize = filesize($filePath);//坑 filesize 如果超過2G 低版本php會返回負(fù)數(shù)    

header('Content-Length:' . $fileSize); //注意是'Content-Length:' 非Accept-Length

header('Content-Transfer-Encoding: binary');

readfile('test.zip');

對當(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)證的登陸對話框

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')

實(shí)際使用:

<?php 
ini_set('memory_limit', '1024M'); // 設(shè)置php可以使用的內(nèi)存大小為 1G

set_time_limit(0); // 超時(shí)時(shí)間不限制
 
$filename = DOCUMENT_ROOT . 'logs/start/start.zip';// 存儲位置(DOCUMENT_ROOT【ci框架】)
 
// ###### 這里是下載zip文件  ###########

header('Content-Description: File Transfer'); // 響應(yīng)頭: 文件傳輸

header('Content-Type: application/octet-stream');  // 聲明輸出的是二進(jìn)制字節(jié)流

header('Content-Disposition: attachment; filename=企業(yè)信息.zip'); // 文件名

header('Content-Transfer-Encoding: binary');  // 告訴瀏覽器,這是二進(jìn)制文件

header('Expires: 0'); // 在代理服務(wù)器端防止緩沖

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');// must-revalidate打開新窗口訪問時(shí)都會重新訪問服務(wù)器,pre-check擴(kuò)展名定義了這樣一段時(shí)間間隔(以秒記)

// 即post-check=0,pre-check=0是IE5.0才有的防cache聲明
header('Pragma: public');// public 針對ie 相當(dāng)于  no-cache

header('Content-Length: ' . filesize($filename)); // 描述HTTP消息實(shí)體的傳輸長度

ob_clean();   // 清空(擦掉)輸出緩沖區(qū)

flush();      // 刷新輸出緩沖

readfile($filename); // 輸出文件

exit;
endding.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容