2010-08-01 98 views
1

對於PHP而言,解碼$ _POST作爲application/x-www-form-urlencoded通過AJAX發送是否正常?PHP不解碼POST

我想使用urldecode,但它擺脫了我的+字符。幫幫我?

15:51:55.490[755ms][total 755ms] Status: 200[OK] 
POST Load Flags[LOAD_BYPASS_CACHE LOAD_BACKGROUND ] Content Size[72] Mime Type[text/html] 
    Request Headers: 
     Host[] 
     User-Agent[Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8] 
     Accept[text/html, */*] 
     Accept-Language[en-us,en;q=0.5] 
     Accept-Encoding[gzip,deflate] 
     Accept-Charset[ISO-8859-1,utf-8;q=0.7,*;q=0.7] 
     Keep-Alive[115] 
     Proxy-Connection[keep-alive] 
     Content-Type[application/x-www-form-urlencoded; charset=UTF-8] 
     X-Requested-With[XMLHttpRequest] 
     Content-Length[164] 
     Pragma[no-cache] 
     Cache-Control[no-cache] 
    Post Data: 
     id[133] 
     content[%253Cp%253E%250A%2509Test%2520test%2520-%2520this%2520will%2520disappear%253A%2520%2B%253C%2Fp%253E%250A] 
     title[Plus%2520character%2520not%2520working] 
    Response Headers: 
     Via[1.1 KIGALI] 
     Connection[Keep-Alive] 
     Proxy-Connection[Keep-Alive] 
     Content-Length[72] 
     Date[Mon, 02 Aug 2010 03:51:55 GMT] 
     Content-Type[text/html; charset=utf-8] 
     Server[Apache/2.2.12 (Ubuntu)] 
     X-Powered-By[PHP/5.2.10-2ubuntu6.4] 
     Vary[Accept-Encoding] 
     Keep-Alive[timeout=15, max=100] 
+0

這是urldecode所做的(直接從doc: *加號('+')被解碼爲空格字符*)。請張貼一些示例代碼。不,$ _POST對於常規請求或ajax請求將以相同的方式工作。 – Ben 2010-08-01 23:39:31

+0

不,這是不正常的提問,而不帶一些例子讓別人明白你在說什麼 – 2010-08-02 02:45:06

+0

由於在發佈數據中有正確編碼爲'%2B'的'+'符號,請顯示您的PHP代碼。 – 2010-08-02 03:57:46

回答

3

部分urldecode的功能是將+標誌變爲空格。爲了解決這個問題,你應該進行urlencode字符串中的任何+的跡象,所以他們變成%2B

所以:

<?php 
$theTag = 'c++'; 
urldecode($theTag); 
    // output is "c " 
$theTag = urlencode($theTag); 
urldecode($theTag); 
    // output is "c++" 
?> 

現在,the superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results。我不確定$_POST。但它也可能被解碼。


從你的問題,我不知道你正在處理什麼用,但是看着在PHP手冊上urldecode的評論,here is a comment you may be interested in

It's worth pointing out that if you are using AJAX and need to encode strings that are being sent to a PHP application, you may not need to decode them in PHP.

<?php 
echo stripslashes(nl2br($_POST['message'])); 
?> 

Will properly output a message sent with the javascript code if the message is encoded:

message = encodeURIComponent(message) 

And is sent with an AJAX POST request with the header:

ajaxVar.setRequestHeader('Content-type', 'application/x-www-form-urlencoded') 
2

PHP確實解碼您的數據。問題是它是雙重編碼的。

在第二個變量:

 
[email protected]:~$ php -r "echo urldecode(urldecode('%253Cp%253E%250A%2509Test%25 
20test%2520-%2520this%2520will%2520disappear%253A%2520%2B%253C%2Fp%253E%250A') 
);" 
<p> 
     Test test - this will disappear: </p> 

正如你所看到的,你必須調用urldecode兩次。所以在Javascript方面出現了一些問題,這使得它被雙重編碼。


這是正常的PHP不解碼$ _ POST發送,應用/通過Ajax的X WWW的形式,進行了urlencoded?

不,它當然不是。在這裏發佈HTTP請求標頭,如果它不起作用,那麼我們可以嘗試查看請求是否有問題。

我想使用urldecode,但它擺脫了我的+字符。幫幫我?

您可以改爲使用rawurldecode。不過,我不明白你爲什麼需要這個。

+0

我添加了我的請求標題。謝謝:) – 2010-08-02 03:53:54

+0

@David我會看看我是否可以重現該問題。 – Artefacto 2010-08-02 04:35:55

+0

@David我編輯了我的回覆。 – Artefacto 2010-08-02 05:06:33

1

這是我的解決方案,我遇到了同樣的問題。

JavaScript代碼來發送數據:

var content = '<br>%20``+++dqwcdwdw';  
content = urlencode(content); 
content = encodeURIComponent(content); 

然後將數據通過POST發送和 ("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");

這裏是PHP代碼接收數據:

$content = urldecode($_POST['thepostfieldname']); 
$content = stripslashes(nl2br($content)); 

這適用於我:)