php中的header

來源:互聯網
上載者:User

最近作東西涉及到header及一些http協議之間的東西,重新看了下php手冊(絕對是個好東西),更明白了一些東西,記下先,可能還會補充:

手冊摘入:

header
發送一個原始 HTTP 標題 (PHP 3, PHP 4, PHP 5)
void header ( string string [, bool replace [, int http_response_code]] )

具體各參數就看php手冊了,說得很詳細,這次我用到的是header("Location: http://ovliverlin.cnblogs.com");這個功能,這樣實現跳轉在單獨空白頁面是沒問題的,但是如果不是手冊所說得情況“要記住 header() 必須在任何實際輸出之前調用,不論是來自普通的 HTML 標籤,空行或者 PHP。”就會產生header already sented的錯誤提示,且不會執行跳轉,在web端可以使用<script>location.href = "abc.php"</script>代替,但是如果無法實現js的wap端呢?而或必須使用header的情況呢?這樣可以使用緩衝來解決。手冊中引用“可以在代碼中使用 ob_start()ob_end_flush() 來實現這樣的功能,或者通過修改 php.ini 中的 output_buffering 配置選項來實現,也可以通過修改伺服器設定檔來實現。”
如下:
ob_start();
<html>
<head></head>
<body>
.....
<?php
header("Location: http://ovliverlin.cnblogs.com");
?>
</body>
</html>

手冊中的ob_start()解釋是:
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer
也就是它可以緩衝所有的頁面內容(除header)標題,即:這樣就使得header優先於其他輸出。

這種用法有個注意:
注意:

Microsoft Internet Explorer 4.01 中的一個漏洞使得該機制無法正常工作,無解決方案。在 Microsoft Internet Explorer 5.5 中也有個漏洞影響到這一點,升級到 Service Pack 2 或更高版本可以解決。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.