代码之家  ›  专栏  ›  技术社区  ›  David Lawson

PHP不解码POST

php
  •  1
  • David Lawson  · 技术社区  · 16 年前

    PHP不解码通过AJAX作为application/x-www-form-urlencoded发送的$\u POST是正常的吗?

    :

    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]
    
    3 回复  |  直到 16 年前
        1
  •  3
  •   Peter Ajtai    16 年前

    部分 urldecode 的功能是 + 标志进入空间。 为了解决这个问题,你应该对任何 + 你的绳子上有符号,所以它们变成了 %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
  •  2
  •   Artefacto    16 年前

    PHP确实可以解码您的数据。问题是它是双重编码的。

    glopes@nebm:~$  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


    我想使用urldecode,但它摆脱了我的+字符。救命啊?

    你可以用 rawurldecode 相反。但是,我不明白你为什么需要这个。

        3
  •  1
  •   bfavaretto    13 年前

    这是我的解决办法,我也有同样的问题。

    发送数据的JavaScript代码:

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

    然后通过邮寄和 ("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");

    下面是接收数据的php代码:

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

    推荐文章