麻辣家族討論版版's Archiver

小誌 發表於 2011-4-19 09:39

5-4-1 網頁轉向

[color=DarkRed][size=4][b]5-4-1   網頁轉向[/b][/size][/color]
網頁資訊「Location」可轉向其他網頁或網址,這個方法也就所謂的『自動鏈結』:
header(“Locatiom: 欲前往的網址”);
例如:[code]<?
//載入網頁後,自動轉接鍊結至與主網頁同一目錄中的message.htm網頁
header("Location: message.htm");
//載入主網頁後,自動轉接鍊結至主網頁上一層目錄中的ex04_01.asp網頁
header("Location: ../ch4/ex04_01.php");
//載入主網頁後,自動轉接鍊結至與另一網址中的網頁
header("Location: http://www.twbts.com");
?>[/code]header()函數只可用在尚未有任何資料輸出至瀏覽器之前才可呼叫。尚未有任何資料輸出至瀏覽器之前才可呼叫?這是什麼意思?就是在程式開端符號『<?』之前都不能有任何資料啦!如下例:(範例ex05_10.php)[code]<html><body>
<%
Response.Redirect "message.htm"
%>
</body>
</html>[/code]上例結果將會獲得下圖的錯誤訊息:
[attach]5521[/attach]
圖5-15  範例ex05_16.php。

小誌 發表於 2011-4-19 09:40

[color=Magenta]實做學習[/color]:
在留言板範例中(go2php,message2.htm),若表單中的任一欄位沒有填寫資料都會顯示「姓名或email或留言,任何一欄位不可為空白,請重新輸入」,然後結束程式的執行,我們現在將錯誤訊息的顯示更改為「自動轉向到輸入表單的網頁」:(範例goredirect.php配合redirect.htm)[code]…略
if ($name=="" || $email=="" || $memo=="")
{
  header("Location: redirect.htm");
}
else…略[/code]

bubaby0608 發表於 2013-3-25 23:43

[code]
<?
//載入網頁後,自動轉接鍊結至與主網頁同一目錄中的message.htm網頁
header("Location: message.htm");
//載入主網頁後,自動轉接鍊結至主網頁上一層目錄中的ex04_01.php網頁
header("Location: ../ch4/ex04_01.php");
//載入主網頁後,自動轉接鍊結至與另一網址中的網頁
header("Location: http://www.twbts.com");
?>[/code]

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供