PHP開發常見的頁面跳轉技術

1.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和“:”號間不能有空格,否則不會跳轉。
注意:在用header前不能有任何的輸出。
< ?php
//重定向瀏覽器
header("Location: http://blog.sina.com.cn/u/2515407924");   
//確保重定向後,後續代碼不會被執行   
exit;  

?>  


2:利用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.sina.com.cn/u/2515407924";   
?>   
<html>     
<head>     
<meta http-equiv="refresh" content="1;url=<?php echo $url; ?>">     
</head>     
<body>   
Its transit station.  
</body>  
</html> 
<?php  
$url = "http://blog.sina.com.cn/u/2515407924";
?>
<html>  
<head>  
<meta http-equiv="refresh" content="1;url=<?php echo $url; ?>">  
</head>  
<body>
Its transit station.
</body>
</html>

3:利用javascript的location.href

<?php    
$url = "http://blog.sina.com.cn/u/2515407924";  
echo "<script type=text/javascript>";  
echo "window.location.href=$url";  
echo "</script>";   
?>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章