php之header的不同用法總結(jié)(實(shí)例講解)
1、header()函數(shù)的作用是:發(fā)送一個(gè)原始 HTTP 標(biāo)頭[Http Header]到客戶(hù)端。
header(string,replace,http_response_code) /*string:必需。規(guī)定要發(fā)送的報(bào)頭字符串。 replace:可選。指示該報(bào)頭是否替換之前的報(bào)頭,或添加第二個(gè)報(bào)頭。 默認(rèn)是 true(替換)。false(允許相同類(lèi)型的多個(gè)報(bào)頭)。 http_response_code:可選。把 HTTP 響應(yīng)代碼強(qiáng)制為指定的值。*/
注意:必須在任何實(shí)際的輸出被發(fā)送之前調(diào)用 header() 函數(shù)。
2、 用法1:跳轉(zhuǎn)頁(yè)面
header("Location:https://baidu.com"); //正常跳轉(zhuǎn) header('Refresh: 3; url=https://www.baidu.com'); //3s后跳轉(zhuǎn) //在header作跳轉(zhuǎn)時(shí),避免發(fā)生錯(cuò)誤后,代碼繼續(xù)執(zhí)行,一般加個(gè)exit;
用法2:聲明content-type(我經(jīng)常拿來(lái)決解亂碼)
header('content-type:text/html;charset=utf-8');
用法3:返回響應(yīng)狀態(tài)碼
header('HTTP/1.1 403 Forbidden');
用法4:執(zhí)行下載操作(隱藏文件的位置)
header('Content-Type: application/octet-stream'); //設(shè)置內(nèi)容類(lèi)型 header('Content-Disposition: attachment; filename="example.zip"');//設(shè)置MIME用戶(hù)作為附件 header('Content-Transfer-Encoding: binary'); //設(shè)置傳輸方式 header('Content-Length: '.filesize('example.zip')); //設(shè)置內(nèi)容長(zhǎng)度
用法5:控制瀏覽器緩存
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); //如果服務(wù)器上的網(wǎng)頁(yè)經(jīng)常變化,就把它設(shè)置為-1,表示立即過(guò)期 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); header( 'Cache-Control: no-store, no-cache, must-revalidate' ); header( 'Cache-Control: post-check=0, pre-check=0', false ); header( 'Pragma: no-cache' );
用法6:
3、更多實(shí)例
<?php // ok header('HTTP/1.1 200 OK'); //設(shè)置一個(gè)404頭: header('HTTP/1.1 404 Not Found'); //設(shè)置地址被永久的重定向 header('HTTP/1.1 301 Moved Permanently'); //轉(zhuǎn)到一個(gè)新地址 header('Location: http://www.example.org/'); //文件延遲轉(zhuǎn)向: header('Refresh: 10; url=http://www.example.org/'); print 'You will be redirected in 10 seconds'; //當(dāng)然,也可以使用html語(yǔ)法實(shí)現(xiàn) // <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'); //文檔語(yǔ)言 header('Content-language: en'); //告訴瀏覽器最后一次修改時(shí)間 $time = time() - 60; // or filemtime($fn), etc header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器文檔內(nèi)容沒(méi)有發(fā)生改變 header('HTTP/1.1 304 Not Modified'); //設(shè)置內(nèi)容長(zhǎng)度 header('Content-Length: 1234'); //設(shè)置為一個(gè)下載類(lèi)型 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'); // 對(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'); // Date in the past header('Pragma: no-cache'); //設(shè)置內(nèi)容類(lèi)型: 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-shockwave-flash'); //Flash動(dòng)畫(huà) //顯示登陸對(duì)話框 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'; ?>
以上這篇php之header的不同用法總結(jié)(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Laravel Reponse響應(yīng)客戶(hù)端示例詳解
這篇文章主要給大家介紹了關(guān)于Laravel Reponse響應(yīng)客戶(hù)端的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09ECshop 遷移到 PHP7版本時(shí)遇到的兼容性問(wèn)題
最近有網(wǎng)友問(wèn)我在php7上安裝ecshopv2.7.3時(shí),報(bào)錯(cuò),究竟了半天沒(méi)有找到原因,下面由腳本之家小編給大家分析此問(wèn)題出現(xiàn)的原因2016-02-02php過(guò)濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總
這篇文章主要介紹了php過(guò)濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總,本文使用代碼實(shí)例給出了過(guò)濾HTML內(nèi)容的正則表達(dá)式,具體說(shuō)明請(qǐng)參閱代碼中的注釋,本文對(duì)使用PHP做采集的朋友有比較大的作用,需要的朋友可以參考下2014-09-09php簡(jiǎn)單中獎(jiǎng)算法(實(shí)例)
下面小編就為大家?guī)?lái)一篇php簡(jiǎn)單中獎(jiǎng)算法(實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08CodeIgniter讀寫(xiě)分離實(shí)現(xiàn)方法詳解
這篇文章主要介紹了CodeIgniter讀寫(xiě)分離實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了CodeIgniter讀寫(xiě)分離的相關(guān)配置與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01Laravel框架Blade模板簡(jiǎn)介及模板繼承用法分析
這篇文章主要介紹了Laravel框架Blade模板簡(jiǎn)介及模板繼承用法,結(jié)合實(shí)例形式分析了Laravel框架Blade模板功能及模板繼承相關(guān)操作技巧,需要的朋友可以參考下2019-12-12