#### PHP 中 header 用法詳解
header is used to send raw HTTP headers. See the HTTP/1.1 specification for more information on HTTP headers.
參考:http://php.net/manual/en/function.header.php
范例一:
~~~
<!--?PHP
Header("Location: http://www.jb51.net";);
exit;//在每個重定向之后都必須加上“exit",避免發生錯誤后,繼續執行。
?-->
~~~
~~~
<!--?php
header("refresh:2;url=http://www.jb51.net");
echo "正在加載,請稍等...<br-->三秒后自動跳轉至百度...";
?-->
~~~
范例三:
讓使用者的瀏覽器出現找不到檔案的信息,正確的寫法是:
~~~
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內容,就是說在這些函數的前面不能有任何文字、空行、回車等,而且最好在header()函數后加上exit()函數。
類似的還有 `setcookie()` 和 `session` 函數,這些函數需要在輸出流中增加消息頭部信息。如果在`header()`執行之前有`echo`等語句,當后面遇到`header()`時,就會報出 “Warning: Cannot modify header information - headers already sent by ….”錯誤。
例如,下面的正確寫法:
~~~
<!--?php
header("http/1.1 403 Forbidden");
exit();
?-->
~~~
* * * * *
~~~
<!--?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.baidu.com');
//文件延遲轉向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
//當然,也可以使用html語法實現
// <cke:encoded-->%3Cmeta%20http-equiv%3D%22refresh%22%20content%3D%2210%3Bhttp%3A%2F%2Fwww.example.org%2F%20%2F%3E
// 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';
?-->
~~~
- 數組
- 深拷貝
- 數組操作
- 交/并/合
- 差值
- 搜索
- 排序
- 貨幣
- 字符串
- 補全
- heredoc
- session
- 注銷
- 限時自動注銷
- 日期時間
- 日期時間
- 某月首尾
- 格式判斷
- 年齡計算
- 時間函數
- strtotime
- 版本升級
- $HTTP_RAW_POST_DATA
- 正則函數等
- 版本比較
- php5.3 PHP5.4 PHP5.5 php5.6 新特性
- PHP注釋
- URL地址
- 頁面超時
- 類方法
- 靜態方法
- 子類靜態方法
- 普通方法和靜態方法
- call_user_func 和 call_user_func_array
- 函數
- 動態函數
- 數組循環
- Math函數
- 取整
- 除數余數
- 運行模式
- CLI 模式
- 常用CLI命令
- 檢測環境支持php_cli模式?
- 調試
- 調試工具
- FirePHP
- ChromePHP
- 內置服務器
- 500 錯誤處理
- 命令行
- 遠程操作
- HTTP
- HTTP headers
- 用例
- 保留字
- 教程
- PHP實戰教程-0基礎快速入門
- 接口與抽象類
- 抽象類與接口的區別
- 魔術方法
- 私有屬性方法
- 構造/析構方法
- PHP Error 和 Logging 函數
- 類
- const