溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在PHP項目中利用Header進行頁面跳轉(zhuǎn)時需要注意哪些問題

發(fā)布時間:2020-12-10 15:35:55 來源:億速云 閱讀:132 作者:Leah 欄目:開發(fā)技術(shù)

在PHP項目中利用Header進行頁面跳轉(zhuǎn)時需要注意哪些問題?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

一、location和“:”號間不能有空格,否則會出錯。

二、在用header前不能有任何的輸出。

三、header后的PHP代碼還會被執(zhí)行。

下面是和asp中重定向response.redirect的比較:

例1:

response.redirect "../test.asp"
header("location:../test.php");

兩者區(qū)別:

asp的redirect函數(shù)可以在向客戶發(fā)送頭文件后起作用.


<html><head></head><body>
<%response.redirect "../test.asp"%>
</body></html>

查是php中下例代碼會報錯:

<html><head></head><body>
<&#63;
header("location:../test.php");
&#63;>
</body></html>

只能這樣:

<&#63;
header("location:../test.php");
&#63;>
<html><head></head><body>...</body></html>

即header函數(shù)之前不能向客戶發(fā)送任何數(shù)據(jù).

例2:

asp中

<html><head></head><body>
<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>
</body></html>

結(jié)果是重定向a.asp文件.

php呢&#63;

<&#63;
header("location:../a.php");
header("location:../b.php");
&#63;>
<html><head></head><body></body></html>

我們發(fā)現(xiàn)它重定向b.php.

原來在asp中執(zhí)行redirect后不會再執(zhí)行后面的代碼.

而php在執(zhí)行header后,繼續(xù)執(zhí)行下面的代碼.

在這方面上php中的header重定向不如asp中的重定向.有時我們要重定向后,不能執(zhí)行后面的代碼:

一般地我們用

if(...)
header("...");
else
{
...
}

但是我們可以簡單的用下面的方法:

if(...)
{ header("...");exit();}

還要注意的是,如果是用Unicode(UTF-8)編碼時也會出現(xiàn)問題,需要調(diào)整緩存設(shè)置.

<[email=%@]%@LANGUAGE="VBSCRIPT[/email]" CODEPAGE="936"%>
<%if Request.ServerVariables("SERVER_NAME")="s.jb51.net" then
response.redirect "news/index.htm"
else%>
<%end if%>
<script>
var url = location.href;
if(url.indexOf('http://www.devdao.com/')!=-1)location.href='/index/index.htm';
if(url.indexOf('http://www.knowsky.com/')!=-1)location.href='/index1/index.htm';
if(url.indexOf('http://www.google.com/')!=-1)location.href='/cn/index.asp';
if(url.indexOf('http://www.baidu.com/')!=-1)location.href='/cn/index.asp';
</script>

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI