最近作東西涉及到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 或更高版本可以解決。