PHP header 的7種用法


PHP header 的7種用法

1. 跳轉頁面

1

header('Location:'.$url);  //Location和":"之間無空格。

2. 聲明content-type

1

header('content-type:text/html;charset=utf-8');

3. 返回response狀態碼

1

header('HTTP/1.1 404 Not Found');

4. 在某個時間后執行跳轉

1

header('Refresh: 10; url=http://www.baidu.com/');  //10s后跳轉。

5. 控制瀏覽器緩存

1

2

3

4

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");

6. . 執行http驗證

1

2

header('HTTP/1.1 401 Unauthorized');

header('WWW-Authenticate: Basic realm="Top Secret"');

7. 執行下載操作

1

2

3

4

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')); //設置內容長度


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM