PHP Header用于頁面跳轉(zhuǎn)時的幾個注意事項(xiàng)
前言
本文介紹的是在PHP中用header("location:test.php")進(jìn)行跳轉(zhuǎn)要注意以下幾點(diǎn),有助于解決一些新手經(jīng)常遇到的問題
一、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中下例代碼會報(bào)錯:
<html><head></head><body>
<?
header("location:../test.php");
?>
</body></html>
只能這樣:
<?
header("location:../test.php");
?>
<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呢?
<?
header("location:../a.php");
header("location:../b.php");
?>
<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>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望能對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
c#中的實(shí)現(xiàn)php中的preg_replace
最近在按照一個php項(xiàng)目用c#重寫,一邊學(xué)習(xí)同時發(fā)現(xiàn)了他們的神似神不似的很多地方2009-12-12
PHP獲取網(wǎng)頁所有連接的方法(附demo源碼下載)
這篇文章主要介紹了PHP獲取網(wǎng)頁所有連接的方法,涉及PHP基于curl針對網(wǎng)頁的操作技巧,并帶附demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03
PHP和JAVA中的重載(overload)和覆蓋(override) 介紹
PHP不支持方法和操作符重載。JAVA不支持操作符的重載(但是“+”實(shí)際上是一種操作符重載)2012-03-03

