2010-08-01 4 views
1

PHP가 AJAX를 통해 application/x-www-form-urlencoded로 보낸 $ _POST를 디코딩하지 않는 것이 정상입니까?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가하는 것입니다 (바로 문서에서 : * 더하기 기호 ('+')는 공백 문자로 디코딩됩니다. *). 샘플 코드를 게시하십시오. 그리고 아니요, $ _POST는 정규 또는 아약스 요청에 대해 동일한 방식으로 작동합니다. – Ben

+0

아니요, 정상적인 질문이 아닙니다. 다른 사람들이 무슨 말을하는지 이해할 수 있도록 예제를 가져 오지 마십시오 –

+0

POST 데이터에 '% 2B'로 올바르게 인코딩 된 '+'기호가 있기 때문에 PHP 코드를 보여주십시오. –

답변

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에 대해 확실하지 않습니다. 그러나 잘 디코딩 될 수도 있습니다. 귀하의 질문에서


, 난 당신이 here is a comment you may be interested in, 처리하지만, PHP 설명서에 URLDECODE에 대한 의견을 찾고 정확히 모르겠어요 :

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를 호출해야합니다. 그래서 자바 스크립트쪽에 뭔가 잘못되어 두 배로 인코딩됩니다.


그것은 PHP 애플리케이션/AJAX 통해 x-www-form-urlencoded로 전송 $ _POST를 디코딩하지 않는 정상인가?

아니요, 확실히 아닙니다. HTTP 요청 헤더가 작동하지 않는다면 여기에 POST가 표시되므로 요청에 문제가 있는지 확인할 수 있습니다.

urldecode를 사용하고 싶지만 내 + 문자를 제거합니다. 도움?

대신 rawurldecode을 사용할 수 있습니다. 그러나 나는 왜 이것이 필요한지 이해하지 못한다.

+0

요청 헤더를 추가했습니다. 감사합니다 :) –

+0

@ David 나는 문제를 재현 할 수 있는지 보겠습니다. – Artefacto

+0

@ David 내 답변을 편집했습니다. – Artefacto

1

여기 내 해결책이 있습니다. 동일한 문제가있었습니다.

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)); 

자바 스크립트 코드는 데이터를 전송하기 나 :)

관련 문제