日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php header运用细节

發布時間:2024/4/14 php 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php header运用细节 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.111cn.net/phper/php-function/55872.htm

http://blog.sina.com.cn/s/blog_7298f36f01011dxv.html

header的用法

header()函數的作用是:發送一個原始 HTTP 標頭[Http Header]到客戶端。

標頭 (header) 是服務器以 HTTP 協義傳 HTML 資料到瀏覽器前所送出的字串,在標頭

與 HTML 文件之間尚需空一行分隔。有關 HTTP 的詳細說明,可以參 RFC 2068 官方文件

(http://www.w3.org/Protocols/rfc2068/rfc2068)。

在 PHP 中送回 HTML 資料前,需先 傳完所有的標頭。

使用范例

范例一: 本例使瀏覽器重定向到 PHP 的官方網站。

<?PHP

Header("Location: http://www.php.net";);

exit; //在每個重定向之后都必須加上“exit",避免發生錯誤后,繼續執行。

?>

<?php

header("refresh:3;url=http://axgle.za.net");

print('正在加載,請稍等...<br>三秒后自動跳轉~~~');

header重定向 就等價于替用戶在地址欄輸入url

?>

范例二:禁止頁面在IE中緩存

要使用者每次都能得到最新的資料,而不是 Proxy 或 cache 中的資料,可以使用下列的標頭

<?PHP

header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );

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' ); //兼容http1.0和https

?>

CacheControl = no-cache

Pragma=no-cache

Expires = -1

Expires是個好東東,如果服務器上的網頁經常變化,就把它設置為-1,表示立即過期。如果一個網頁每天凌晨1點更新,可以把Expires設置為第二天的凌晨1點。

當HTTP1.1服務器指定CacheControl = no-cache時,瀏覽器就不會緩存該網頁。

舊式 HTTP 1.0 服務器不能使用 Cache-Control 標題。所以為了向后兼容 HTTP 1.0 服務器,IE使用Pragma:no-cache 標題對 HTTP 提供特殊支持。

如果客戶端通過安全連接 (https://) 與服務器通訊,且服務器在響應中返回 Pragma:no-cache 標題,則 Internet Explorer 不會緩存此響應。

注意:Pragma:no-cache 僅當在安全連接中使用時才防止緩存,如果在非安全頁中使用,處理方式與 Expires:-1 相同,該頁將被緩存,但被標記為立即過期。

http-equiv meta標記:

在html頁面中可以用http-equiv meta來標記指定的http消息頭部。老版本的IE可能不支持html meta標記,所以最好使用http消息頭部來禁用緩存。

范例三: 讓使用者的瀏覽器出現找不到檔案的信息。

網上很多資料這樣寫:php的函數header()可以向瀏覽器發送Status標頭,

如 header(”Status: 404 Not Found”)。

但是我發現實際上瀏覽器返回的響應卻是:

HTTP/1.x 200 OK

Date: Thu, 03 Aug 2006 07:49:11 GMT

Server: Apache/2.0.55 (Win32) PHP/5.0.5

X-Powered-By: PHP/5.0.5

Status: 404 Not Found

Content-Length: 0

Keep-Alive: timeout=15, max=98

Connection: Keep-Alive

Content-Type: text/html

查了一些資料,正確的寫法是:

header(”http/1.1 404 Not Found”);

第一部分為HTTP協議的版本(HTTP-Version);第二部分為狀態代碼(Status);第三部分為原因短語(Reason-Phrase)。

范例四:讓使用者下載檔案( 隱藏文件的位置 )

html標簽 就可以實現普通文件下載。如果為了保密文件,就不能把文件鏈接告訴別人,可以用header函數實現文件下載。

<?php

header("Content-type: application/x-gzip");

header("Content-Disposition: attachment; filename=文件名\");

header("Content-Description: PHP3 Generated Data");

?>

范例四:header函數前輸入內容

一般來說在header函數前不能輸出html內容,類似的還有setcookie() 和 session 函數,這些函數需要在輸出流中增加消息頭部信息。如果在header()執行之前有echo等語句,當后面遇到header()時,就會報出 “Warning: Cannot modify header information - headers already sent by ….”錯誤。就是說在這些函數的前面不能有任何文字、空行、回車等,而且最好在header()函數后加上exit()函數。例如下面的錯誤寫法,在兩個 php代碼段之間有一個空行:

//some code here

?>

//這里應該是一個空行

header(”http/1.1 403 Forbidden”);

exit();

?>

原因是:PHP腳本開始執行 時,它可以同時發送http消息頭部(標題)信息和主體信息. http消息頭部(來自 header() 或 SetCookie() 函數)并不會立即發送,相反,它被保存到一個列表中. 這樣就可以允許你修改標題信息,包括缺省的標題(例如 Content-Type 標題).但是,一旦腳本發送了任何非標題的輸出(例如,使用 HTML 或 print() 調用),那么PHP就必須先發送完所有的Header,然后終止 HTTP header.而后繼續發送主體數據.從這時開始,任何添加或修改Header信息的試圖都是不允許的,并會發送上述的錯誤消息之一。

解決辦法:

修改php.ini打開緩存(output_buffering),或者在程序中使用緩存函數ob_start(),ob_end_flush()等。原 理是:output_buffering被啟用時,在腳本發送輸出時,PHP并不發送HTTP header。相反,它將此輸出通過管道(pipe)輸入到動態增加的緩存中(只能在PHP 4.0中使用,它具有中央化的輸出機制)。你仍然可以修改/添加header,或者設置cookie,因為header實際上并沒有發送。當全部腳本終止 時,PHP將自動發送HTTP header到瀏覽器,然后再發送輸出緩沖中的內容。

=================================================================

PHP 手冊實例應用

1:您可以使用heder命令,強制使瀏覽器使用新鮮的內容(無緩存) 。

也可以給網址增加了一個唯一的編號,使其每次都讀取新的內容,避免緩存。

example:

<?

print "<img src='yourfile.jpg'>"; //通常讀取的是緩存文件

?>

<?

print "<img src='yourfile.jpg?".time()."'>"; //增加了唯一的編號,使瀏覽器重新請求

w//print "<img src='yourfile.jpg?".rand(100,999)."'>";

?>

2: 下面是個很好的函數,將圖片傳送給瀏覽器顯示。

<?php

function PE_img_by_path($PE_imgpath = "")

{

if (file_exists($PE_imgpath)) {

$PE_imgarray = pathinfo($PE_imgpath);

$iconcontent = file_get_contents($PE_imgpath);

header("Content-type: image/" . $PE_imgarray["extension"]);

header('Content-length: ' . strlen($iconcontent));

echo $iconcontent;

die(0);

}

return false;

}

?>

更多的實例:

<?php

// ok

header('HTTP/1.1 200 OK');

//設置一個404頭:

header('HTTP/1.1 404 Not Found');

//設置地址被永久的重定向

header('HTTP/1.1 301 Moved Permanently');

//轉到一個新地址

header('Location: http://www.example.org/');

//文件延遲轉向:

header('Refresh: 10; url=http://www.example.org/');

print 'You will be redirected in 10 seconds';

//當然,也可以使用html語法實現

// <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');

//文檔語言

header('Content-language: en');

//告訴瀏覽器最后一次修改時間

$time = time() - 60; // or filemtime($fn), etc

header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

//告訴瀏覽器文檔內容沒有發生改變

header('HTTP/1.1 304 Not Modified');

//設置內容長度

header('Content-Length: 1234');

//設置為一個下載類型

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');

// 對當前文檔禁用緩存

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');

//設置內容類型:

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動畫

//顯示登陸對話框

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';

?>

轉自旭日達網絡


header() 函數向客戶端發送原始的 HTTP 報頭,主要包括有HTTP協議的版本、狀態代碼、原因短語等我們常用于跳轉頁面,狀態發送與文件下載,下面我們一起來看看。

header分為三部分:

第一部分為HTTP協議的版本(HTTP-Version);
第二部分為狀態代碼(Status);
第三部分為原因短語(Reason-Phrase)。

header()函數使用說明:?

一、作用:???
~~~~~~~~~???
?????? PHP只是以HTTP協議將HTML文檔的標頭送到瀏覽器,告訴瀏覽器具體怎么處理這個頁面,至于傳送的內容則需要熟悉一下HTTP協議了,與PHP無關了,可參照http://www.w3.org/Protocols/rfc2616/rfc2616。???
?????? 傳統的標頭一定包含下面三種標頭之一,并只能出現一次。???
?????? Location:? xxxx:yyyy/zzzz???
?????? Content-Type:? xxxx/yyyy???
?????? Status:? nnn? xxxxxx?

二、先來了解一下HTTP協議的運作方式???
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~???
?????? HTTP協議是基于請求/響應范式的。一個客戶機與服務器建立連接后,發送一個請求給服務器,請求方式的格式為,統一資源標識符、協議版本號,后邊是MIME信息包括請求修飾符、客戶機信息和可能的內容。服務器接到請求后,給予相應的響應信息,其格式為一個狀態行包括信息的協議版本號、一個成功或錯誤的代碼,后邊是MIME信息包括服務器信息、實體信息和可能的內容。???
?????? 它分四個過程,在HTTP協議中,服務端是指提供HTTP服務的部分,客戶端是指你使用的瀏覽器或者下載工具等等。在通訊時,由客戶端發出請求連接,服務端建立連接;然后,客戶端發出HTTP請求(Request),服務端返回響應信息(Respond),由此完成一個HTTP操作。?

三、HTTP協議狀態碼表示的意思???
~~~~~~~~~~~~~~~~~~~~~~~~~~~~???
1×× 保留???
2×× 表示請求成功地接收???
3×× 為完成請求客戶需進一步細化請求???
4×× 客戶錯誤???
5×× 服務器錯誤?

代碼如下
復制代碼

// fix 404 pages: 用這個header指令來解決URL重寫產生的404 header
header(‘HTTP/1.1 200 OK’);

// set 404 header: 頁面沒找到
header(‘HTTP/1.1 404 Not Found’);

// 頁面被永久刪除,可以告訴seo/seo.html" rel="nofollow" target="_blank">搜索引擎更新它們的urls
// set Moved Permanently header (good for redrictions)
// use with location header
header(‘HTTP/1.1 301 Moved Permanently’);
// 訪問受限
header(‘HTTP/1.1 403 Forbidden’);
// 服務器錯誤
header(‘HTTP/1.1 500 Internal Server Error’);

// 重定向到一個新的位置
// redirect to a new location:
header(‘Location: http://www.m-bang.com);

延遲一段時間后重定向
// 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’);

// 內容語言 (en = English)
// 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’);

// 顯示登錄對話框,可以用來進行HTTP認證
// 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’;
?>

現在表單的填寫,我們可以用AJAX對用戶隨時進行驗證,進行友好的提示,但是在用戶沒有留意AJAX友好提示,提交了錯誤的表單,跳回原頁,而填寫的信息卻全部丟失了。要支持頁面回跳,有以下的辦法:
1. 使用session_cache_limiter方法: session_cache_limiter(‘private,must-revalidate’);但是要值得注意的是 session_cache_limiter()方法要寫在session_start()方法之前才有用;
2.用header來設置控制緩存的方法: header(‘Cache-control:private,must-revalidate’);

頁面跳轉要注意的幾個問題總結

1、location和“:”號間不能有空格,否則會出錯。?
2、在用header前不能有任何的輸出。?
3、header后的PHP代碼還會被執行。

轉載于:https://www.cnblogs.com/ryanlamp/p/4719892.html

總結

以上是生活随笔為你收集整理的php header运用细节的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。