PHP頁面跳轉一、header()函數
header()函數是PHP中進行頁面跳轉的一種十分簡單的方法。header()函數的主要功能是將HTTP協議標頭(header)輸出到瀏覽器。
header()函數的定義如下:
void header (string string [,bool replace [,int http_response_code]])
可選參數replace指明是替換前一條類似標頭還是添加一條相同類型的標頭,默認為替換。
第二個可選參數http_response_code強制將HTTP相應代碼設為指定值。 header函數中Location類型的標頭是一種特殊的header調用,常用來實現頁面跳轉。注意:1.location和“:”號間不能有空格,否則不會跳轉。
2.在用header前不能有任何的輸出。
3.header后的PHP代碼還會被執行。例如
- < ?php
- //重定向瀏覽器
- header("Location: http://blog.csdn.net/abandonship");
- //確保重定向后,后續代碼不會被執行
- exit;
- ?>
PHP頁面跳轉二、Meta標簽
Meta標簽是HTML中負責提供文檔元信息的標簽,在PHP程序中使用該標簽,也可以實現頁面跳轉。若定義http-equiv為refresh,則打開該頁面時將根據content規定的值在一定時間內跳轉到相應頁面。若設置content="秒數;url=網址",則定義了經過多長時間后頁面跳轉到指定的網址。
< meta http-equiv="refresh" content="1;url=http://blog.csdn.net/abandonship">
例,以下程序meta.php實現在該頁面中停留一秒后頁面自動跳轉。
- <?php
- $url = "http://blog.csdn.net/abandonship";
- ?>
- <html>
- <head>
- <meta http-equiv="refresh" content="1;url=<?php echo $url; ?>">
- </head>
- <body>
- It's transit station.
- </body>
- </html>
PHP頁面跳轉三、JavaScript
- <?php
- $url = "http://blog.csdn.net/abandonship";
- echo "<script type='text/javascript'>";
- echo "window.location.href='$url'";
- echo "</script>";
- ?>