2012-04-06 60 views
5

我想通過一個頭變量鏈接。我嘗試過以下代碼PHP傳遞一個變量頭重定向

$name = "mike"; 

if($name != "lopan"){ 
     header("Location:error-page.php?$errorMssg=Please enter information?"); 
} 

此頁面重定向到位置,但不傳遞包含該消息的變量。但是當我創建一個這樣的值的簡單鏈接:

<a href="error-page.php?$errorMssg=so what happened there buddy?">link</a> 

它通過它就好了。

任何想法我做錯了什麼?或者我無法通過標題傳遞信息?

+1

你不需要URL中的$字符 – GordonM 2012-04-06 20:12:26

回答

11

您需要使用urlencode這樣的:

if($name != "lopan"){ 
     header("Location:error-page.php?errorMssg=".urlencode("Waoo so your not EVEN going to try to enter information huh?")); 
} 

而且在錯誤page.php文件,你應該得到它(不需要urldecode):

<?php 
$errorMssg = $_GET['errorMssg']; 
+0

Lols,這是一個標題的事情,只要不在鏈接中添加「$」或?因爲當我做了一個正常的鏈接,它通過很好。 – somdow 2012-04-06 20:16:18

+2

在URL中傳遞參數時,您不需要任何'$'''。你需要一個'?'來表示參數的開頭,以及每個之間的'&',像這樣:'http://example.com?foo = bar&baz = baz'。不要將它與以'$'開頭的PHP變量混淆。 – 2012-04-06 20:21:09

5

刪除$之前errorMssgurlencode的消息。

1

難道是因爲你有$errorMssg而不是$errorMsg?也嘗試製作一個有效的網址,例如用%20等替換「」,函數urlencode()可以幫助你做到這一點。