PHP頁面3中跳轉方法

來源:互聯網
上載者:User

方法一:使用PHP內建函數

Header("Location:
網址

");


說明:必須在網頁沒有任何輸出的時候執行,要特別要注意空格。去空格的方法參照如下第4問

即,這句話要放在網頁開始的時候,放在body裡或後面都會出錯,

 

 

方法二:利用meta

echo "<meta http-equiv=refresh content='0; url=網址
'>";

說明:沒有方法一的限制,但是如果前面有輸出,則輸出的內容會閃爍一下然後進入跳轉到的頁面。

如:

$post="guoqing.php?id=".urlencode("$id") ."&name=".urlencode($name);  

//加urlencode()函數在 地址欄則不顯示$a變數的中文真實內容,用%……%……來表示。

echo "<meta http-equiv=refresh content='0; url=$post'>"; //頁面跳躍陳述式這樣可實現頁面傳值


方法三:利用Javascript語言

echo "<script language='javascript'>";
echo " location='網址
';";
echo "</script>";

4:為什麼我向另一網頁傳送變數時,只得到前半部分,以空格開頭的則全部丟失

CODE: <?

php
$Var
=
"hello php"
;

//修改為$Var="      hello php";試試得到什麼結果

$post
=
"receive.php?Name="
.
$Var

;

header
(
"location:$post"

);
?>


非常全面的一個php技術網站,php愛好者站

http://www.phpfans.net

有相當豐富的文章和原始碼.
receive.php的內容:

CODE: <?

PHP

Echo
"<pre>"

;
Echo   
$_GET
[
"Name"

];
Echo
"</pre>"

;
?>


非常全面的一個php技術網站,php 愛好者站

http://www.phpfans.net

有相當豐富的文章和原始碼.
正確的方法是:

CODE: <?

php
$Var
=
"hello php"

;

$post
=
"receive.php?Name="
.
urlencode
(
$Var

);

header
(
"location:$post"

);
?>


php愛好者站

http://www.phpfans.net

為phper提供一切資訊.
在接收頁面你不需要使用Urldecode(),變數會自動編碼.

 

在PHP中用header("location:test.php")進行跳轉要注意以下幾點:

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

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

3、header後的PHP代碼還會被執行。

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

例1:
response.redirect "../test.asp"
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函數之前不能向客戶發送任何資料.

例2:
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("...");exit();}

還要注意的是,如果是用Unicode(UTF-8)編碼時也會出現問題,需要調整緩衝設定.

 

 

 

<%@LANGUAGE="VBSCRIPT

" CODEPAGE="936"%>
<%if Request.ServerVariables("SERVER_NAME")="news.sina.com" then
response.redirect "news/index.htm"
else%>
<%end if%>

 

 

 

<script>
var url = location.href;
if(url.indexOf('http://www.zqpc.net.cn/')!=-1)location.href='/index/index.htm';
if(url.indexOf('http://www.zqtj.net/')!=-1)location.href='/index1/index.htm';
if(url.indexOf('http://www.xxfww.net.cn/')!=-1)location.href='/cn/index.asp';
if(url.indexOf('http://xxfww.net.cn/')!=-1)location.href='/cn/index.asp';
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.