PHP header函數(shù)分析詳解
更新時(shí)間:2011年08月06日 19:36:36 作者:
PHP只是以HTTP協(xié)議將HTML文檔的標(biāo)頭送到瀏覽器,告訴瀏覽器具體怎么處理這個(gè)頁(yè)面,至于傳送的內(nèi)容則需要熟悉一下HTTP協(xié)議了,與PHP無(wú)關(guān)
在php語(yǔ)言中,header()這個(gè)函數(shù)很有用的,尤其在用到ajax時(shí)候,他會(huì)幫你解決一些意想不到的問(wèn)題。下面是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簡(jiǎn)單實(shí)現(xiàn)“相關(guān)文章推薦”功能的方法
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)“相關(guān)文章推薦”功能的方法,方法簡(jiǎn)單功能實(shí)用,需要的朋友可以參考下2014-07-07圖文詳解phpstorm配置Xdebug進(jìn)行調(diào)試PHP教程
這篇文章主要為大家詳細(xì)的介紹了phpstorm配置Xdebug進(jìn)行調(diào)試PHP教程 ,感興趣的小伙伴們可以參考一下2016-06-06Windows下IIS6/Apache2.2.4+MySQL5.2+PHP5.2.1安裝配置方法
Windows下IIS6/Apache2.2.4+MySQL5.2+PHP5.2.1安裝配置方法...2007-05-05淺析關(guān)于PHP位運(yùn)算的簡(jiǎn)單權(quán)限設(shè)計(jì)
本篇文章是對(duì)PHP位運(yùn)算的簡(jiǎn)單權(quán)限設(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06破解圖片防盜鏈的代碼(asp/php)測(cè)試通過(guò)
許多的網(wǎng)站圖片都加了防盜鏈措施,常見(jiàn)的新浪博客、網(wǎng)易相冊(cè)、百度空間、QQ空間都不能外鏈圖片,如果要先把圖片保存到本地再上傳,不免顯得麻煩。2010-07-07對(duì)Session和Cookie的區(qū)分與解釋
對(duì)Session和Cookie的區(qū)分與解釋...2007-03-03