PHP header函數設置http報文頭示例詳解以及解決http返回頭中content-length與Transfer-Encoding: chunked的問題


最近在服務器上,多媒體與設備(攝像頭)對接的時候,總是發生錯誤導致設備崩潰,抓包發現響應頭不對,沒有返回length,使得攝像頭立即崩潰。找了一下資料,改了一下響應頭就好了。

//定義編碼 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/atom+xml'); //CSS header('Content-type: text/css'); //Javascript header('Content-type: text/javascript'); //JPEG Image header('Content-type: image/jpeg'); //JSON header('Content-type: application/json'); //PDF header('Content-type: application/pdf'); //RSS header('Content-Type: application/rss+xml; charset=ISO-8859-1'); //Text (Plain) header('Content-type: text/plain'); //XML header('Content-type: text/xml'); //200 OK header('HTTP/1.1 200 OK'); //設置一個404頭: header('HTTP/1.1 404 Not Found'); //設置地址被永久的重定向 header('HTTP/1.1 301 Moved Permanently'); //轉到一個新地址 header('Location: http://www.example.org/'); //文件延遲轉向: header('Refresh: 10; url=http://www.example.org/'); print 'You will be redirected in 10 seconds'; //當然,也可以使用html語法實現 //<meta http-equiv="refresh" content="10;http://www.example.org/ /> //override X-Powered-By: PHP: header('X-Powered-By: PHP/4.4.0'); header('X-Powered-By: Brain/0.6b'); //文檔語言 header('Content-language: en'); //告訴瀏覽器最后一次修改時間 $time = time() - 60; // or filemtime($fn), etc header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器文檔內容沒有發生改變 header('HTTP/1.1 304 Not Modified'); //設置內容長度 header('Content-Length: 1234'); //設置為一個下載類型 header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="example.zip"'); header('Content-Transfer-Encoding: binary'); //load the file to send: 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'); // Date in the past 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'); //JPG*** header('Content-Type: application/zip'); // ZIP文件 header('Content-Type: application/pdf'); // PDF文件 header('Content-Type: audio/mpeg'); // 音頻文件 header('Content-Type: application/x-shockw**e-flash'); //Flash動畫 //顯示登陸對話框 header('HTTP/1.1 401 Unauthorized'); header('WWW-Authenticate: Basic realm="Top Secret"'); print 'Text that will be displayed if the user hits cancel or '; print 'enters wrong login data';



#附:HTTP header 詳解

Requests部分

Header 解釋 示例
Accept 指定客戶端能夠接收的內容類型 Accept: text/plain, text/html
Accept-Charset 瀏覽器可以接受的字符編碼集。 Accept-Charset: iso-8859-5
Accept-Encoding 指定瀏覽器可以支持的web服務器返回內容壓縮編碼類型。 Accept-Encoding: compress, gzip
Accept-Language 瀏覽器可接受的語言 Accept-Language: en,zh
Accept-Ranges 可以請求網頁實體的一個或者多個子范圍字段 Accept-Ranges: bytes
Authorization HTTP授權的授權證書 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Cache-Control 指定請求和響應遵循的緩存機制 Cache-Control: no-cache
Connection 表示是否需要持久連接。(HTTP 1.1默認進行持久連接) Connection: close
Cookie HTTP請求發送時,會把保存在該請求域名下的所有cookie值一起發送給web服務器。 Cookie: $Version=1; Skin=new;
Content-Length 請求的內容長度 Content-Length: 348
Content-Type 請求的與實體對應的MIME信息 Content-Type: application/x-www-form-urlencoded
Date 請求發送的日期和時間 Date: Tue, 15 Nov 2010 08:12:31 GMT
Expect 請求的特定的服務器行為 Expect: 100-continue
From 發出請求的用戶的Email From: user@email.com
Host 指定請求的服務器的域名和端口號 Host: www.zcmhi.com
If-Match 只有請求內容與實體相匹配才有效 If-Match: “737060cd8c284d8af7ad3082f209582d”
If-Modified-Since 如果請求的部分在指定時間之后被修改則請求成功,未被修改則返回304代碼 If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT
If-None-Match 如果內容未改變返回304代碼,參數為服務器先前發送的Etag,與服務器回應的Etag比較判斷是否改變 If-None-Match: “737060cd8c284d8af7ad3082f209582d”
If-Range 如果實體未改變,服務器發送客戶端丟失的部分,否則發送整個實體。參數也為Etag If-Range: “737060cd8c284d8af7ad3082f209582d”
If-Unmodified-Since 只在實體在指定時間之后未被修改才請求成功 If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT
Max-Forwards 限制信息通過代理和網關傳送的時間 Max-Forwards: 10
Pragma 用來包含實現特定的指令 Pragma: no-cache
Proxy-Authorization 連接到代理的授權證書 Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Range 只請求實體的一部分,指定范圍 Range: bytes=500-999
Referer 先前網頁的地址,當前請求網頁緊隨其后,即來路 Referer: http://www.zcmhi.com/archives/71.html
TE 客戶端願意接受的傳輸編碼,並通知服務器接受接受尾加頭信息 TE: trailers,deflate;q=0.5
Upgrade 向服務器指定某種傳輸協議以便服務器進行轉換(如果支持) Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
User-Agent User-Agent的內容包含發出請求的用戶信息 User-Agent: Mozilla/5.0 (Linux; X11)
Via 通知中間網關或代理服務器地址,通信協議 Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning 關於消息實體的警告信息 Warn: 199 Miscellaneous warning

Responses 部分  

Header 解釋 示例
Accept-Ranges 表明服務器是否支持指定范圍請求及哪種類型的分段請求 Accept-Ranges: bytes
Age 從原始服務器到代理緩存形成的估算時間(以秒計,非負) Age: 12
Allow 對某網絡資源的有效的請求行為,不允許則返回405 Allow: GET, HEAD
Cache-Control 告訴所有的緩存機制是否可以緩存及哪種類型 Cache-Control: no-cache
Content-Encoding web服務器支持的返回內容壓縮編碼類型。 Content-Encoding: gzip
Content-Language 響應體的語言 Content-Language: en,zh
Content-Length 響應體的長度 Content-Length: 348
Content-Location 請求資源可替代的備用的另一地址 Content-Location: /index.htm
Content-MD5 返回資源的MD5校驗值 Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
Content-Range 在整個返回體中本部分的字節位置 Content-Range: bytes 21010-47021/47022
Content-Type 返回內容的MIME類型 Content-Type: text/html; charset=utf-8
Date 原始服務器消息發出的時間 Date: Tue, 15 Nov 2010 08:12:31 GMT
ETag 請求變量的實體標簽的當前值 ETag: “737060cd8c284d8af7ad3082f209582d”
Expires 響應過期的日期和時間 Expires: Thu, 01 Dec 2010 16:00:00 GMT
Last-Modified 請求資源的最后修改時間 Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT
Location 用來重定向接收方到非請求URL的位置來完成請求或標識新的資源 Location: http://www.zcmhi.com/archives/94.html
Pragma 包括實現特定的指令,它可應用到響應鏈上的任何接收方 Pragma: no-cache
Proxy-Authenticate 它指出認證方案和可應用到代理的該URL上的參數 Proxy-Authenticate: Basic
refresh 應用於重定向或一個新的資源被創造,在5秒之后重定向(由網景提出,被大部分瀏覽器支持)

 

Refresh: 5; url=

http://www.zcmhi.com/archives/94.html

Retry-After 如果實體暫時不可取,通知客戶端在指定時間之后再次嘗試 Retry-After: 120
Server web服務器軟件名稱 Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
Set-Cookie 設置Http Cookie Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
Trailer 指出頭域在分塊傳輸編碼的尾部存在 Trailer: Max-Forwards
Transfer-Encoding 文件傳輸編碼 Transfer-Encoding:chunked
Vary 告訴下游代理是使用緩存響應還是從原始服務器請求 Vary: *
Via 告知代理客戶端響應是通過哪里發送的 Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning 警告實體可能存在的問題 Warning: 199 Miscellaneous warning
WWW-Authenticate 表明客戶端請求實體應該使用的授權方案 WWW-Authenticate: Basic
 
http返回頭中content-length與Transfer-Encoding: chunked的問題釋疑
在用firebug查看http響應頭的時候,有時候發現有content- length屬性,有時候沒有這個屬性,取而代之的是Transfer-Encoding: chunked屬性。由於客戶端強制要求,服務器端必須返回content-length,否則,客戶端將不予解析。於是測試發現,當文件很小的時 候,是有content-length屬性的,到達一個值時,就成了Tansfer-Encoding:chunked。
 
再引用一段話,解釋一下Transfer-Encoding:chunked這個屬性的意義:
        通常,HTTP協議中使用Content-Length這個頭來告知數據的長度。然后,在數據下行的過程中,Content-Length的方式要預先在服務器中緩存所有數據,然后所有數據再一股腦兒地發給客戶端。
如果要一邊產生數據,一邊發給客戶端,WEB 服務器就需要使用"Transfer-Encoding: chunked"這樣的方式來代替Content-Length。
 
解決方法:
1 告知服務器不要緩存,或於apache/nginx配置緩存段,將緩存提高。
2 手動設置響應頭,告知content-length


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM