佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > php之header的不同用法總結(jié)(實(shí)例講解)

php之header的不同用法總結(jié)(實(shí)例講解)

熱門(mén)標(biāo)簽:銀行業(yè)務(wù) 服務(wù)器配置 Mysql連接數(shù)設(shè)置 團(tuán)購(gòu)網(wǎng)站 電子圍欄 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Linux服務(wù)器

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è)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 探討php中header的用法詳解
  • php header功能的使用
  • PHP 常用的header頭部定義匯總

標(biāo)簽:蚌埠 衡水 衢州 廣元 江蘇 棗莊 萍鄉(xiāng) 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php之header的不同用法總結(jié)(實(shí)例講解)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266
    兴隆县| 仪征市| 宁明县| 湟中县| 抚远县| 津市市| 永胜县| 手游| 福州市| 克拉玛依市| 奉化市| 阿克陶县| 纳雍县| 黄山市| 四会市| 商都县| 乌拉特前旗| 景泰县| 南陵县| 郓城县| 水富县| 湟中县| 东城区| 新巴尔虎右旗| 青岛市| 长岭县| 化德县| 灵台县| 蕲春县| 延寿县| 平顶山市| 清徐县| 荔波县| 西充县| 博兴县| 乌苏市| 沙洋县| 丰台区| 顺平县| 桓台县| 陇南市|