方法一:使用PHP自帶函數
Header("Location: 網(wǎng)址");
說(shuō)明:必須在網(wǎng)頁(yè)沒(méi)有任何輸出的時(shí)候執行,要特別要注意空格。去空格的方法參照如下第4問(wèn)
即,這句話(huà)要放在網(wǎng)頁(yè)開(kāi)始的時(shí)候,放在body里或后面都會(huì )出錯,
方法二:利用meta
echo "<meta http-equiv=refresh content='0; url=網(wǎng)址'>";
說(shuō)明:沒(méi)有方法一的限制,但是如果前面有輸出,則輸出的內容會(huì )閃爍一下然后進(jìn)入跳轉到的頁(yè)面。
如:
$post="guoqing.php?id=".urlencode("$id") ."&name=".urlencode($name);
//加urlencode()函數在 地址欄則不顯示$a變量的中文真實(shí)內容,用%……%……來(lái)表示。
echo "<meta http-equiv=refresh content='0; url=$post'>"; //頁(yè)面跳轉語(yǔ)句這樣可實(shí)現頁(yè)面傳值
方法三:利用Javascript語(yǔ)言
echo "<script language='javascript'>";
echo " location='網(wǎng)址';";
echo "</script>";
在PHP中用header("location:test.php")進(jìn)行跳轉要注意以下幾點(diǎn):
1、location和“:”號間不能有空格,否則會(huì )出錯。
2、在用header前不能有任何的輸出。
3、header后的PHP代碼還會(huì )被執行。
下面是和asp中重定向response.redirect的比較:
例1:
response.redirect "../test.asp"
header("location:../test.php");
兩者區別:
asp的redirect函數可以在向客戶(hù)發(fā)送頭文件后起作用.
如
<html><head></head><body>
<%response.redirect "../test.asp"%>
</body></html>
查是php中下例代碼會(huì )報錯:
<html><head></head><body>
<?
header("location:../test.php");
?>
</body></html>
只能這樣:
<?
header("location:../test.php");
?>
<html><head></head><body>...</body></html>
即header函數之前不能向客戶(hù)發(fā)送任何數據.
例2:
asp中
<html><head></head><body>
<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>
</body></html>
結果是重定向a.asp文件.
php呢?
<?
header("location:../a.php");
header("location:../b.php");
?>
<html><head></head><body></body></html>
我們發(fā)現它重定向b.php.
原來(lái)在asp中執行redirect后不會(huì )再執行后面的代碼.
而php在執行header后,繼續執行下面的代碼.
在這方面上php中的header重定向不如asp中的重定向.有時(shí)我們要重定向后,不能執行后面的代碼:
一般地我們用
if(...)
header("...");
else
{
...
}
但是我們可以簡(jiǎn)單的用下面的方法:
if(...)
{ header("...");exit();}
還要注意的是,如果是用Unicode(UTF-8)編碼時(shí)也會(huì )出現問(wèn)題,需要調整緩存設置.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%if Request.ServerVariables("SERVER_NAME")="news.sina.com" then
response.redirect "news/index.htm"
else%>
<%end if%>
<script>
var url = location.href;
if(url.indexOf('http://www.zqpc.net.cn/')!=-1)location.href='/index/index.htm';
if(url.indexOf('http://www.zqtj.net/')!=-1)location.href='/index1/index.htm';
if(url.indexOf('http://www.xxfww.net.cn/')!=-1)location.href='/cn/index.asp';
if(url.indexOf('http://xxfww.net.cn/')!=-1)location.href='/cn/index.asp';
</script>
聯(lián)系客服