
header()函數(shù)的主要功能是將HTTP協(xié)議標(biāo)頭(header)輸出到瀏覽器。
語法:
void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) 可選參數(shù)replace指明是替換前一條類似標(biāo)頭還是添加一條相同類型的標(biāo)頭,默認(rèn)為替換。
第二個(gè)可選參數(shù)http_response_code強(qiáng)制將HTTP相應(yīng)代碼設(shè)為指定值。 header函數(shù)中Location類型的標(biāo)頭是一種特殊的header調(diào)用,常用來實(shí)現(xiàn)頁面跳轉(zhuǎn)。注意:1.location和“:”號(hào)間不能有空格,否則不會(huì)跳轉(zhuǎn)。
舉例:
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標(biāo)簽是HTML中負(fù)責(zé)提供文檔元信息的標(biāo)簽,在PHP程序中使用該標(biāo)簽,也可以實(shí)現(xiàn)頁面跳轉(zhuǎn)。 若定義http-equiv為refresh,則打開該頁面時(shí)將根據(jù)content規(guī)定的值在一定時(shí)間內(nèi)跳轉(zhuǎn)到相應(yīng)頁面。
1 2 3 | < meta http-equiv="refresh"content="1;url=http://www.baidu.com"> |
通過使用windows.location.href=‘url’; 是頁面自動(dòng)跳轉(zhuǎn)到新的地址
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>"; ?> |