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

PHP中用header的具體使用技巧

開發(fā) 后端
PHP中用header會或多或少的遇到一些問題,比如:“:”號間不能有空格;在用header前不能有任何的輸出;PHP中用header后的PHP代碼還會被執(zhí)行等等。

我們在PHP中用header的實際應(yīng)用中會或多或少的遇到一些問題,下面我們就向大家介紹一些在進行跳轉(zhuǎn)的時候遇到的一些問題的具體解決辦法,有助于新手學(xué)習(xí)。

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

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

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

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

例1:

response.redirect "../test.asp"

header("location:../test.php");

兩者區(qū)別:

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

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

查是PHP中用header中下例代碼會報錯:

  1. <html><head></head><body> 
  2. <? 
  3. header("location:../test.php");  
  4. ?> 
  5. </body></html> 

只能這樣:

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

即PHP中用header函數(shù)之前不能向客戶發(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> 

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

php呢?

  1. <? 
  2. header("location:../a.php");  
  3. header("location:../b.php");  
  4. ?> 
  5. <html><head></head><body></body></html> 

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

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

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

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

一般地我們用

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

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

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

PHP中用header還要注意的是,如果是用Unicode(UTF-8)編碼時也會出現(xiàn)問題,需要調(diào)整緩存設(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> 
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-11-25 17:28:26

PHP對話

2009-12-01 10:50:45

PHP函數(shù)requir

2009-12-01 14:26:19

PHP函數(shù)ob_sta

2009-11-24 20:09:35

PHP函數(shù)header

2009-12-02 13:53:12

PHP使用技巧

2009-12-04 10:19:11

PHP hack

2009-11-30 09:21:39

PHP函數(shù)rmdir(

2009-12-07 16:59:53

PHP匹配顏色函數(shù)

2009-12-01 19:23:22

PHP緩存技術(shù)

2009-12-02 18:51:12

PHP分頁類

2009-11-30 13:15:27

PHP模板Smarty

2009-11-27 13:14:07

PHP函數(shù)strist

2009-12-07 16:52:59

PHP函數(shù)getima

2009-11-26 10:23:17

2009-11-24 15:50:09

PHP上傳類uploa

2009-11-26 09:18:56

PHP函數(shù)參數(shù)傳遞方法

2009-11-24 19:25:32

PHP關(guān)聯(lián)數(shù)組

2009-11-26 19:05:04

PHP函數(shù)explod

2009-11-26 15:23:24

PHP函數(shù)ereg()

2009-11-25 10:02:27

PHP會話Sessio
點贊
收藏

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