PHP header函數(shù)分析詳解
更新時(shí)間:2011年08月06日 19:36:36 作者:
PHP只是以HTTP協(xié)議將HTML文檔的標(biāo)頭送到瀏覽器,告訴瀏覽器具體怎么處理這個(gè)頁(yè)面,至于傳送的內(nèi)容則需要熟悉一下HTTP協(xié)議了,與PHP無關(guān)
在php語(yǔ)言中,header()這個(gè)函數(shù)很有用的,尤其在用到ajax時(shí)候,他會(huì)幫你解決一些意想不到的問題。下面是header的一些詳細(xì)講解。希望對(duì)phper有幫助
<?php
// fix 404 pages:
header('HTTP/1.1 200 OK');
// set 404 header:
header('HTTP/1.1 404 Not Found');
// set Moved Permanently header (good for redrictions)
// use with location header
header('HTTP/1.1 301 Moved Permanently');
// redirect to a new location:
header('Location: http://www.example.org/');
// redrict with delay:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
// you could also use the HTML syntax:// <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');
// content language (en = English)
header('Content-language: en');
// 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');
// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');
// set content length (good for caching):
header('Content-Length: 1234');
// Headers for an download:
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');
// 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');
// 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');
// 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 data';
?>
復(fù)制代碼 代碼如下:
<?php
// fix 404 pages:
header('HTTP/1.1 200 OK');
// set 404 header:
header('HTTP/1.1 404 Not Found');
// set Moved Permanently header (good for redrictions)
// use with location header
header('HTTP/1.1 301 Moved Permanently');
// redirect to a new location:
header('Location: http://www.example.org/');
// redrict with delay:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
// you could also use the HTML syntax:// <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');
// content language (en = English)
header('Content-language: en');
// 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');
// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');
// set content length (good for caching):
header('Content-Length: 1234');
// Headers for an download:
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');
// 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');
// 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');
// 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 data';
?>
相關(guān)文章
php通過header增加Key、Sign和Timestamp實(shí)現(xiàn)鑒權(quán)機(jī)制的流程步驟
在現(xiàn)代Web應(yīng)用程序中,鑒權(quán)是確保只有合法用戶能夠訪問資源的關(guān)鍵部分,在PHP中,你可以通過在HTTP請(qǐng)求的Header中添加Key、Sign和Timestamp來實(shí)現(xiàn)鑒權(quán)機(jī)制,需要的朋友可以參考下2023-10-10
PHP數(shù)組的交集array_intersect(),array_intersect_assoc(),array_inte
求兩個(gè)數(shù)組的交集問題可以使用array_intersect(),array_inersect_assoc,array_intersect_key來實(shí)現(xiàn),其中array_intersect()函數(shù)是求兩個(gè)數(shù)的交集2011-05-05
基于PHP實(shí)現(xiàn)棧數(shù)據(jù)結(jié)構(gòu)和括號(hào)匹配算法示例
這篇文章主要介紹了基于PHP實(shí)現(xiàn)棧數(shù)據(jù)結(jié)構(gòu)和括號(hào)匹配算法,結(jié)合實(shí)例形式分析了php數(shù)組操作實(shí)現(xiàn)棧數(shù)據(jù)結(jié)構(gòu)的進(jìn)棧、出棧,以及基于棧的括號(hào)匹配應(yīng)用技巧,需要的朋友可以參考下2017-08-08
PHP取得一個(gè)類的屬性和方法的實(shí)現(xiàn)代碼
PHP取得一個(gè)類的屬性和方法的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-05-05

