PHP和ASP中關于轉向函數的區別
asp中實現重定向是用response.redirect函數:
用法一例: response.redirect '../test.asp'
php中也有類似函數:header
用法一例: 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函數之前不能向客戶發送任何數據.
再看下面一例:
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('...');b
reak;}
相關文章: