2010-08-01 97 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)); 

这适用于我:)