2009年12月8日 星期二

php函數-header


php函數-header
header
(PHP 4, PHP 5)
header — 送出一個新的http的header訊息
什麼是http的header訊息呢?
就是’送出給伺服器的訊息’
而伺服器會因為這些訊息而回傳不同的資訊
例如:錯誤訊息,網址…之類的。
舉在php.net上的例子
1
2
3
4
5
6
7
8
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
就可以將網頁用pdf方式儲存,但是…不能閱讀,因為資料不是pdf格式
舉我常用的例子…說實在話也不常用
指定網頁編碼
1
2
3
header("content-type:text/html; charset=UTF-8");
?>
但是要注意的是…
使用header時,必須在網頁輸出內容之前,不然就會出現錯誤。
這裡還有’POST送出值的例子’
1
2
3
4
5
6
7
8
9
10
11
12
13
$host = "www.example.com";
$path = "/path/to/script.php";
$data = "data1=value1&data2=value2";
$data = urlencode($data);
 
header("POST $path HTTP/1.1\r\n" );
header("Host: $host\r\n" );
header("Content-type: application/x-www-form-urlencoded\r\n" );
header("Content-length: " . strlen($data) . "\r\n" );
header("Connection: close\r\n\r\n" );
header($data);
?>
這個有時候跟一些系統介接是相當好用的語法
給大家做個參考。

1 則留言: