自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用PHP header進(jìn)行頁(yè)面跳轉(zhuǎn)出現(xiàn)的問(wèn)題解疑

開(kāi)發(fā) 后端
PHP header在使用的過(guò)程需要注意一些問(wèn)題,在這篇文章中我們將為大家詳細(xì)的介紹這些問(wèn)題,以便大家牢固掌握PHP header的含義。

我們?cè)谶M(jìn)行頁(yè)面跳轉(zhuǎn)的時(shí)候,通常會(huì)用到PHP header。那么如何才能正確的使用PHP header呢?我們?cè)谑褂玫倪^(guò)程中應(yīng)當(dāng)注意些什么問(wèn)題呢?#t#

在PHP中用header("location:test.php")進(jìn)行跳轉(zhuǎn)要注意以下幾點(diǎn),有助于解決一些新手經(jīng)常遇到的問(wèn)題

1、location和“:”號(hào)間不能有空格,否則會(huì)出錯(cuò)。
2、在用header前不能有任何的輸出。
3、header后的PHP代碼還會(huì)被執(zhí)行。

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

例1:

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

兩者區(qū)別:

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

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

查是php中下例代碼會(huì)報(bào)錯(cuò):

  1. < html>< head>< /head>< body> 
  2. <? 
  3. header("location:../test.php");  
  4. ?> 
  5. < /body>< /html> 
  6. 只能這樣:  
  7. < ?   
  8. header("location:../test.php");  
  9. ?> 
  10. < html>< head>< /head>< body>...< /body>< /html> 

即PHP header之前不能向客戶(hù)發(fā)送任何數(shù)據(jù).
例2:

asp中

  1. < html>< head>< /head>< body> 
  2. <%  
  3. response.redirect "../a.asp"  
  4. response.redirect "../b.asp"  
  5. %> 
  6. < /body>< /html> 
  7. 結(jié)果是重定向a.asp文件.  
  8. php呢?  
  9. < ?  
  10. header("location:../a.php");  
  11. header("location:../b.php");  
  12. ?> 
  13. < html>< head>< /head>< body>< /body>< /html> 

我們發(fā)現(xiàn)它重定向b.php.
原來(lái)在asp中執(zhí)行redirect后不會(huì)再執(zhí)行后面的代碼.
而php在執(zhí)行header后,繼續(xù)執(zhí)行下面的代碼.
在這方面上php中的header重定向不如asp中的重定向.有時(shí)我們要重定向后,不能執(zhí)行后面的代碼:
一般地我們用

  1. if(...)  
  2. header("...");  
  3. else  
  4. {  
  5. ...  
  6. }  

但是我們可以簡(jiǎn)單的用下面的PHP header方法:

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

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

  1. < [email=%@]%@LANGUAGE="VBSCRIPT[/email]" CODEPAGE="936"%> 
  2. < %if Request.ServerVariables("SERVER_NAME")="s.jb51.net" then  
  3. response.redirect "news/index.htm"  
  4. else%> 
  5. < %end if%> 
  6. < script> 
  7. var url = location.href;  
  8. if(url.indexOf('http://www.jb51.net/')!=-1)location.href='/index/index.htm';  
  9. if(url.indexOf('http://www.kanshule.com/')!=-1)location.href='/index1/index.htm';  
  10. if(url.indexOf('http://www.shouji17.com/')!=-1)location.href='/cn/index.asp';  
  11. if(url.indexOf('http://www.baidu.com/')!=-1)location.href='/cn/index.asp';  
  12. < /script>  

以上就是在使用PHP header時(shí)應(yīng)當(dāng)注意的幾點(diǎn)問(wèn)題。

責(zé)任編輯:曹凱 來(lái)源: 網(wǎng)易博客
相關(guān)推薦

2009-12-07 10:05:22

PHP session

2009-12-01 15:41:16

PHP substr截

2010-03-15 17:17:29

Java線(xiàn)程池

2009-12-08 10:33:23

PHP應(yīng)用問(wèn)題

2009-12-03 15:13:51

PHP開(kāi)發(fā)中問(wèn)題

2009-02-04 18:20:42

服務(wù)器虛擬化VMware

2009-12-31 15:28:02

Silverlight

2010-01-04 13:15:33

跨域引用Silverl

2010-02-05 16:07:52

C++多態(tài)覆蓋

2010-02-02 13:20:33

C++對(duì)象傳遞

2009-11-23 18:47:51

PHP中用header

2015-05-05 10:51:32

php頁(yè)面跳轉(zhuǎn)方法

2010-01-20 14:52:21

VB.NET修改數(shù)據(jù)窗

2009-12-02 19:42:24

PHP頁(yè)面自動(dòng)跳轉(zhuǎn)

2009-12-02 20:02:18

PHP實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)

2009-12-02 20:09:45

PHP頁(yè)面跳轉(zhuǎn)函數(shù)

2009-12-11 13:25:01

PHP頁(yè)面跳轉(zhuǎn)

2009-11-27 16:13:21

PHP遞歸返回值

2009-11-27 10:02:54

PHP mb_strw

2009-12-21 10:19:05

Silverlight
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)