首頁  >  文章  >  後端開發  >  PHP Header用於頁面跳躍的注意事項有哪些

PHP Header用於頁面跳躍的注意事項有哪些

coldplay.xixi
coldplay.xixi原創
2020-08-10 13:42:261884瀏覽

PHP Header用於頁面跳轉的注意事項:1、location和【:】號間不能有空格,否則會出錯;2、在用header前不能有任何的輸出;3、header後的PHP程式碼還會執行。

PHP Header用於頁面跳躍的注意事項有哪些

PHP Header用於頁面跳躍的注意事項:

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

範例1:

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

兩者差異:

asp的redirect函數可以在向客戶發送頭檔後起作用.

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

查是php中下例程式碼會報錯誤:

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

只能這樣:

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

即header函數之前不能向客戶發送任何資料.

例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>

我們發現它重定向b.php.

原來在asp中執行redirect後不會再執行後面的程式碼.

而php在執行header後,繼續執行下面的程式碼.

在這方面上php中的header重定向不如asp中的重定向.有時我們要重定向後,不能執行後面的程式碼:

一般地我們用

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

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

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

還要注意的是,如果是用Unicode(UTF-8)編碼時也會出現問題,需要調整快取設定.

<[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(&#39;http://www.devdao.com/&#39;)!=-1)location.href=&#39;/index/index.htm&#39;;
if(url.indexOf(&#39;http://www.knowsky.com/&#39;)!=-1)location.href=&#39;/index1/index.htm&#39;;
if(url.indexOf(&#39;http://www.google.com/&#39;)!=-1)location.href=&#39;/cn/index.asp&#39;;
if(url.indexOf(&#39;http://www.baidu.com/&#39;)!=-1)location.href=&#39;/cn/index.asp&#39;;
</script>

相關學習推薦:php程式設計(影片)

以上是PHP Header用於頁面跳躍的注意事項有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn