php頁面跳轉的幾種方式
阿新 • • 發佈:2019-01-01
@: PHP頁面跳轉的三種方式
第一種方式:header()
header()函式的主要功能是將HTTP協議標頭(header)輸出到瀏覽器。
語法:
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
可選引數replace指明是替換前一條類似標頭還是新增一條相同型別的標頭,預設為替換。
第二個可選引數http_response_code強制將HTTP相應程式碼設為指定值。 header函式中Location型別的標頭是一種特殊的header呼叫,常用來實現頁面跳轉。注意:1.location和“:”號間不能有空格,否則不會跳轉。
舉例:
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
注意:
- header執行前不能有任何輸出
- location和:之間不能有空格
- header後的php程式碼還會執行,所以需要注意使用exit;
第二種方式:meta標籤
Meta標籤是HTML中負責提供文件元資訊的標籤,在PHP程式中使用該標籤,也可以實現頁面跳轉。 若定義http-equiv為refresh,則開啟該頁面時將根據content規定的值在一定時間內跳轉到相應頁面。
< meta http-equiv="refresh"
content="1;url=http://
www.baidu.com">
第三種方式:javascript
通過使用windows.location.href=‘url’; 是頁面自動跳轉到新的地址
< ?php
$url = "http://www.baidu.com";
echo "< script language='javascript'
type='text/javascript'>";
echo "window.location.href='$url'";
echo "< /script>";
?>
第四種方式:form表單
通過php載入一個html頁面,html頁面中隱藏一個form表單,再寫個js在頁面載入時自動提交form表單達成跳轉
<html>
<head>
<meta charset="utf-8">
</head>
<body onload="load()">
<form id="formCommit" action="http://www.baidu.com" method="post">
<input type="hidden" id = "req" name="req" value="<?php echo htmlspecialchars($param['resp']);?>" />
<input type="hidden" id = "sign" name="sign" value="<?php echo htmlspecialchars($param['sign']);?>" />
</form>
<script type="text/javascript">
function load(){
document.getElementById("formCommit").submit();
}
</script>
</body>
</html>