本文目錄一覽:
Php header()函數語法及使用代碼
語法:
複製代碼
代碼如下:
Void
header(string
$string[,bool
$replace=true
[,
int
$http_response_code)
向客戶端發送原始的HTTP報頭
需注意:
Header函數必須在任何實際的輸出前調用,無論是一般的html標籤、文件中空行,或者來自php。就是在這個函數之前不能有任何形式的輸出。
參數說明:
參數 描述
string 必需。規定要發送的報頭字元串。
replace 可選。指示該報頭是否替換之前的報頭,或添加第二個報頭。
默認是
true(替換)。false(允許相同類型的多個報頭)。
http_response_code 可選。把
HTTP
響應代碼強製為指定的值。(PHP
4
以及更高版本可用)
常見用法實例:
一、發送伺服器狀態碼
複製代碼
代碼如下:
header(‘HTTP/1.0
404
Not
Found’);
常用狀態碼:
狀態碼 說明
100-199 表示成功接收請求,要求客戶端繼續提交下一次請求才能完成整個處理過程
200-299 成功接收請求,並已完成整個處理過程,常用200
300-399 未完成請求,客戶端需要進一步細化請求,比如,請求的資源已經移動到一個新的地址,常用302,304
400-499 客戶端請求有誤
常用404
500-599 伺服器端出現錯誤,常用500
詳細見:HTTPFTP相應提示信息
二、頁面跳轉
複製代碼
代碼如下:
//
立即跳轉
header(‘Location:
‘);
//
3秒後跳轉
header(‘refresh:3;url=’);
3.設置瀏覽器緩存
強制瀏覽器不進行緩存!
[code]
//header(‘Expires:-1’);
header(‘Expires:
Mon,
26
Jul
1997
05:00:00
GMT’);
header(‘Cache-Control:no-cache,must-revalidate’);
header(‘pragma:no-cache’);
header(‘Last-Modified:
‘.
date(‘D,
j
M
Y
H:i:s
T’)
);
php中的header是什麼意思
header() 函數向客戶端發送原始的 HTTP 報頭。
語法
header(string,replace,http_response_code)
參數描述
string 必需。規定要發送的報頭字元串。
replace 可選。指示該報頭是否替換之前的報頭,或添加第二個報頭。
默認是 true(替換)。false(允許相同類型的多個報頭)。
http_response_code 可選。把 HTTP 響應代碼強製為指定的值。(PHP 4 以及更高版本可用)
用於301重定向、網頁跳轉、和控制網頁緩存等
php中的header函數
header(“Location:page.php”);
是為了跳轉到其它頁面。當然是運行到它時才跳過去。
編譯時還是運行時執行?這個問題問在include 與 require合適。。。。
注意區分它與 include 與 require區別
另外補充回答:
==================
頁面跳轉並不能用來阻止程序中其他語句的執行?
頁面已經跳轉了,以前的頁面header語句後的邏輯代碼是會繼續執行的,但是輸出語句是不會執行的。
你可以拿個簡單的例子做實驗:
—————————–
文件 a.php
?php
header(‘Location:b.php’);
echo ‘我是跳轉前的頁面a.php!’;
?
文件 b.php
?php
echo ‘我是跳轉後的頁面b.php!’;
?
運行a.php,發現輸出的是 “我是跳轉後的頁面b.php”
如果有二個header以上的header 則以最後一個為準。
同樣上面的例子:
—————————–
文件 a.php
?php
header(‘Location:b.php’);
header(‘Location:c.php’);
echo ‘我是跳轉前的頁面a.php!’;
?
運行a.php,會跳到c.php
再來個例子證明,邏輯代碼是會執行的。
——–
?php
header(‘Location:b.php’);
$a = ‘c.php’;
header(‘Location:’.$a);
echo ‘我是aa.php’;
?
結果跳到了c.php,證明$a=’c.php’執行了。
———————
總結:
header後,想禁止後面語句執行,你可以加上exit; 或return;
如下
?php
header(‘Location:b.php’);
return;
//exit;
$a = ‘c.php’;
header(‘Location:’.$a);
echo ‘我是a.php’;
?
則只會跳到b.php頁面,不會到c.php.
———————
這個答案,你滿意不?:)
php的header函數設置在哪
//ok
header(『HTTP/1.1 200 OK’);
//設置一個404頭:
header(『HTTP/1.1 404 Not Found’);
//設置地址被永久的重定向
header(『HTTP/1.1 301 Moved Permanently’);
//轉到一個新地址
header(『Location: 『);
//文件延遲轉向:
header(『Refresh: 10; url=『);
print 『You will be redirected in 10 seconds’;
//當然,也可以使用html語法實現
// meta http-equiv=」refresh」 content=」10; /
// 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-shockw**e-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’;
php header 怎麼設置
PHP設置header示例:
?php
//設置頁面編碼
header(“Content-type:/text/html;charset=utf-8;”);
//頁面重定向跳轉示例
header(“location:index.php”);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/253323.html