header() 函數(shù)向客戶端發(fā)送原始的 HTTP 報(bào)頭,主要包括有HTTP協(xié)議的版本、狀態(tài)代碼、原因短語(yǔ)等我們常用于跳轉(zhuǎn)頁(yè)面,狀態(tài)發(fā)送與文件下載,下面我們一起來(lái)看看.
header分為三部分:
第一部分為HTTP協(xié)議的版本(HTTP-Version);
第二部分為狀態(tài)代碼(Status);
第三部分為原因短語(yǔ)(Reason-Phrase);
header()函數(shù)使用說(shuō)明:
一、作用:
PHP只是以HTTP協(xié)議將HTML文檔的標(biāo)頭送到瀏覽器,告訴瀏覽器具體怎么處理這個(gè)頁(yè)面,至于傳送的內(nèi)容則需要熟悉一下HTTP協(xié)議了,與PHP無(wú)關(guān)了,可參照.http://www.w3.org/Protocols/rfc2616/rfc2616.
傳統(tǒng)的標(biāo)頭一定包含下面三種標(biāo)頭之一,并只能出現(xiàn)一次.
- Location: xxxx:yyyy/zzzz
- Content-Type: xxxx/yyyy
- Status: nnn xxxxxx
二、先來(lái)了解一下HTTP協(xié)議的運(yùn)作方式
HTTP協(xié)議是基于請(qǐng)求/響應(yīng)范式的,一個(gè)客戶機(jī)與服務(wù)器建立連接后,發(fā)送一個(gè)請(qǐng)求給服務(wù)器,請(qǐng)求方式的格式為,統(tǒng)一資源標(biāo)識(shí)符、協(xié)議版本號(hào),后邊是MIME信息包括請(qǐng)求修飾符、客戶機(jī)信息和可能的內(nèi)容,服務(wù)器接到請(qǐng)求后,給予相應(yīng)的響應(yīng)信息,其格式為一個(gè)狀態(tài)行包括信息的協(xié)議版本號(hào)、一個(gè)成功或錯(cuò)誤的代碼,后邊是MIME信息包括服務(wù)器信息、實(shí)體信息和可能的內(nèi)容.
它分四個(gè)過(guò)程,在HTTP協(xié)議中,服務(wù)端是指提供HTTP服務(wù)的部分,客戶端是指你使用的瀏覽器或者下載工具等等。在通訊時(shí),由客戶端發(fā)出請(qǐng)求連接,服務(wù)端建立連接,然后,客戶端發(fā)出HTTP請(qǐng)求(Request),服務(wù)端返回響應(yīng)信息(Respond),由此完成一個(gè)HTTP操作.
三、HTTP協(xié)議狀態(tài)碼表示的意思
1×× 保留
2×× 表示請(qǐng)求成功地接收
3×× 為完成請(qǐng)求客戶需進(jìn)一步細(xì)化請(qǐng)求
4×× 客戶錯(cuò)誤
5×× 服務(wù)器錯(cuò)誤
例,代碼如下:
- // fix 404 pages: 用這個(gè)header指令來(lái)解決URL重寫產(chǎn)生的404 header
- header(‘HTTP/1.1 200 OK’);
- // set 404 header: 頁(yè)面沒(méi)找到
- header(‘HTTP/1.1 404 Not Found’);
- // 頁(yè)面被永久刪除,可以告訴seo/seo.html" target="_blank">搜索引擎更新它們的urls
- // set Moved Permanently header (good for redrictions)
- // use with location header
- header(‘HTTP/1.1 301 Moved Permanently’);
- // 訪問(wèn)受限
- header(‘HTTP/1.1 403 Forbidden’);
- // 服務(wù)器錯(cuò)誤
- header(‘HTTP/1.1 500 Internal Server Error’);
- // 重定向到一個(gè)新的位置
- // redirect to a new location:
- header(‘Location: http://www.m-bang.com);
- 延遲一段時(shí)間后重定向
- // redrict with delay:
- header(‘Refresh: 10; url=http://www.sina.com.cn’);
- print ‘You will be redirected in 10 seconds’;
- // 覆蓋 X-Powered-By value
- // override X-Powered-By: PHP:
- header(‘X-Powered-By: PHP/4.4.0′);
- header(‘X-Powered-By: Brain/0.6b’);
- // 內(nèi)容語(yǔ)言 (en = English)
- // content language (en = English)
- header(‘Content-language: en’);
- //最后修改時(shí)間 (在緩存的時(shí)候可以用到)
- // last modified (good for caching)
- $time = time() – 60; // or filemtime($fn), etc
- header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s’, $time).’ GMT’);
- // 告訴瀏覽器要獲取的內(nèi)容還沒(méi)有更新
- // header for telling the browser that the content
- // did not get changed
- header(‘HTTP/1.1 304 Not Modified’);
- // 設(shè)置內(nèi)容的長(zhǎng)度 (緩存的時(shí)候可以用到):
- // set content length (good for caching):
- header(‘Content-Length: 1234′);
- // 用來(lái)下載文件:
- // Headers for an download:
- header(‘Content-Type: application/octet-stream’);
- header(‘Content-Disposition: attachment; filename=”example.zip”‘);
- header(‘Content-Transfer-Encoding: binary’);
- // 禁止緩存當(dāng)前文檔:
- // load the file to send:readfile(‘example.zip’);
- // Disable caching of the current document:
- header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate’);
- header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’);
- // 設(shè)置內(nèi)容類型:
- // Date in the pastheader(‘Pragma: no-cache’);
- // set content type:
- header(‘Content-Type: text/html; charset=iso-8859-1′);
- header(‘Content-Type: text/html; charset=utf-8′);
- header(‘Content-Type: text/plain’);
- // plain text file
- header(‘Content-Type: image/jpeg’);
- // JPG picture
- header(‘Content-Type: application/zip’);
- // ZIP file
- header(‘Content-Type: application/pdf’);
- // PDF file
- header(‘Content-Type: audio/mpeg’);
- // Audio MPEG (MP3,…) file
- header(‘Content-Type: application/x-shockwave-flash’);
- //開源代碼Vevb.com
- // 顯示登錄對(duì)話框,可以用來(lái)進(jìn)行HTTP認(rèn)證
- // Flash animation// show sign in box
- 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 da
- ta’;
現(xiàn)在表單的填寫,我們可以用AJAX對(duì)用戶隨時(shí)進(jìn)行驗(yàn)證,進(jìn)行友好的提示,但是在用戶沒(méi)有留意AJAX友好提示,提交了錯(cuò)誤的表單,跳回原頁(yè),而填寫的信息卻全部丟失了,要支持頁(yè)面回跳,有以下的辦法:
1.使用session_cache_limiter方法:session_cache_limiter(‘private,must-revalidate’);但是要值得注意的是 session_cache_limiter()方法要寫在session_start()方法之前才有用;
2.用header來(lái)設(shè)置控制緩存的方法: header(‘Cache-control:private,must-revalidate’);
頁(yè)面跳轉(zhuǎn)要注意的幾個(gè)問(wèn)題總結(jié):
1、location和“:”號(hào)間不能有空格,否則會(huì)出錯(cuò).
2、在用header前不能有任何的輸出.
3、header后的PHP代碼還會(huì)被執(zhí)行.
下面是和asp中重定向response.redirect的比較:
例1,代碼如下:
response.redirect "../test.asp"
header("location:../test.php");
兩者區(qū)別:
asp的redirect函數(shù)可以在向客戶發(fā)送頭文件后起作用,如代碼如下:
- <html><head></head><body>
- <%response.redirect "../test.asp"%>
- </body></html>
- //查是php中下例代碼會(huì)報(bào)錯(cuò):
- <html><head></head><body>
- <?
- header("location:../test.php");
- ?>
- </body></html>
- //只能這樣:
- <?
- header("location:../test.php");
- ?>
- <html><head></head><body>...</body></html>
即header函數(shù)之前不能向客戶發(fā)送任何數(shù)據(jù).
例2,asp中,代碼如下:
- <html><head></head><body>
- <%
- response.redirect "../a.asp"
- response.redirect "../b.asp"
- %>
- </body></html>
- //結(jié)果是重定向a.asp文件.
- //php呢?
- <?
- header("location:../a.php");
- header("location:../b.php");
- ?>
- <html><head></head><body></body></html>
我們發(fā)現(xiàn)它重定向b.php.
原來(lái)在asp中執(zhí)行redirect后不會(huì)再執(zhí)行后面的代碼.而php在執(zhí)行header后,繼續(xù)執(zhí)行下面的代碼.在這方面上php中的header重定向不如asp中的重定向.有時(shí)我們要重定向后,不能執(zhí)行后面的代碼:一般地我們用如下代碼:
- if(...)
- header("...");
- else
- {
- ...
- }
但是我們可以簡(jiǎn)單的用下面的方法:
- if(...)
- { header("...");exit();}
還要注意的是,如果是用Unicode(UTF-8)編碼時(shí)也會(huì)出現(xiàn)問(wèn)題,需要調(diào)整緩存設(shè)置,代碼如下:
- <[email=%@]%@LANGUAGE="VBSCRIPT[/email]" CODEPAGE="936"%>
- <%if Request.ServerVariables("SERVER_NAME")="s.111cn.net" then
- response.redirect "news/index.htm"
- else%>
- <%end if%>
- <script>
- var url = location.href;
- if(url.indexOf('http://www.companysz.com/')!=-1)location.href='/index/index.htm';
- if(url.indexOf('http://www.zhutiy.com/')!=-1)location.href='/index1/index.htm';
- if(url.indexOf('http://www.companysz.com/')!=-1)location.href='/cn/index.asp';
- if(url.indexOf('http://www.baidu.com/')!=-1)location.href='/cn/index.asp';
- </script>
新聞熱點(diǎn)
疑難解答