header()函數(shù)的主要功能是將HTTP協(xié)議標頭(header)輸出到瀏覽器。
語法:
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
可選參數(shù)replace指明是替換前一條類似標頭還是添加一條相同類型的標頭,默認為替換。
第二個可選參數(shù)http_response_code強制將HTTP相應代碼設為指定值。 header函數(shù)中Location類型的標頭是一種特殊的header調用,常用來實現(xiàn)頁面跳轉。注意:1.location和“:”號間不能有空格,否則不會跳轉。
舉例:
1 2 3 4 5 6 7 | < html > <? php /* This will give an error. Note the output * above, which is before the header() call */ header('Location: http://www.example.com/'); exit; ?> |
注意:
Meta標簽是HTML中負責提供文檔元信息的標簽,在PHP程序中使用該標簽,也可以實現(xiàn)頁面跳轉。 若定義http-equiv為refresh,則打開該頁面時將根據(jù)content規(guī)定的值在一定時間內跳轉到相應頁面。
1 2 3 | < meta http-equiv="refresh" content="1;url=http:// www.baidu.com"> |
通過使用windows.location.href=‘url’; 是頁面自動跳轉到新的地址
1 2 3 4 5 6 7 | < ?php $url = "http://www.baidu.com" ; echo "< script language= 'javascript' type= 'text/javascript' >"; echo "window.location.href='$url'" ; echo "< /script>" ; ?> |