《PHP教程:PHP中header函數的用法及其注意事項詳解》要點:
本文介紹了PHP教程:PHP中header函數的用法及其注意事項詳解,希望對您有用。如果有疑問,可以聯系我們。
PHP實戰
void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) : Send a raw HTTP header
PHP實戰下面有一些使用header的幾種用法:
PHP實戰1、使用header函數進行跳轉頁面;
PHP實戰 header('Location:'.$url);
PHP實戰 其中$url就是將要跳轉的url了.
PHP實戰 這種用法的注意事項有以下幾點:
PHP實戰?Location和":"之間不能有空格,否則會出現錯誤(注釋:我剛測試了,在我本地環境下,沒有跳轉頁面,但是也沒有報錯,不清楚什么原因);
PHP實戰?在用header前不能有任何的輸出(注釋:這點大家都知道的,如果header之前有任何的輸出,包括空白,就會出現header already sent by xxx的錯誤);
PHP實戰?header 后面的東西還會執行的;
PHP實戰2、使用header聲明content-type
PHP實戰 header('content-type:text/html;charset=utf-8');
這個沒有什么好說的;
PHP實戰3、使用header返回response 狀態碼
PHP實戰 header(sprintf('%s %d %s', $http_version, $status_code, $description));
PHP實戰 樣式就是這樣的;
PHP實戰 例如:header('HTTP/1.1 404 Not Found');
PHP實戰4、使用header在某個時間后執行跳轉
PHP實戰 header("Refresh: {$delay}; url={$url}");
PHP實戰 其中$delay就是推遲跳轉的時間,$url為需要跳轉的url
PHP實戰 例如:header('Refresh: 10; url=http://www.example.org/'); 意思為10s后跳轉到http://www.eexample.org這個網站
PHP實戰5、使用header控制瀏覽器緩存
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-cache, must-revalidate");
header("Pragma: no-cache");
PHP實戰6、執行http驗證
PHP實戰 header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
PHP實戰7、使用header進行下載操作
PHP實戰
header('Content-Type: application/octet-stream');//設置內容類型
header('Content-Disposition: attachment; filename="example.zip"'); //設置MIME用戶作為附件下載 如果將attachment換成inline意思為在線打開
header('Content-Transfer-Encoding: binary');//設置傳輸方式
header('Content-Length: '.filesize('example.zip'));//設置內容長度
// load the file to send:
readfile('example.zip');//讀取需要下載的文件
PHP實戰下面再給大家介紹PHP header 的幾種用法
PHP實戰跳轉頁面
PHP實戰header('Location:'.$url); //Location和":"之間無空格.
PHP實戰聲明content-type
PHP實戰header('content-type:text/html;charset=utf-8');
PHP實戰返回response狀態碼
PHP實戰header('HTTP/1.1 404 Not Found');
PHP實戰在某個時間后執行跳轉
PHP實戰header('Refresh: 10; url=http://www.baidu.com/'); //10s后跳轉.
PHP實戰控制瀏覽器緩存
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-cache, must-revalidate");
header("Pragma: no-cache");
PHP實戰執行http驗證
PHP實戰header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
PHP實戰執行下載操作
PHP實戰header('Content-Type: application/octet-stream'); //設置內容類型
header('Content-Disposition: attachment; filename="example.zip"'); //設置MIME用戶作為附件
header('Content-Transfer-Encoding: binary'); //設置傳輸方式
header('Content-Length: '.filesize('example.zip')); //設置內容長度
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6148.html