Percent character replaced in Codeigniter

后端 未结 3 1912
再見小時候
再見小時候 2021-01-13 15:12

In Codeigniter I\'m sending a string using POST method as \"%100\" and it becomes \"0\". I believe this is because they\'re being treated as encode

3条回答
  •  攒了一身酷
    2021-01-13 16:02

    I believe the problem here is nothing to do with CI or even PHP, but with your HTTP request.

    Lets say I make a POST request that looks like this:

    POST /path/to/file HTTP/1.1
    Host: server.com
    User-Agent: Bob the browser/1.0
    Connection: keep-alive
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 25
    
    name=Dave&percentage=%100
    

    What you are probably expecting is for $_POST to look like this:

    Array
        (
            [name] => Dave
            [percentage] => %100
        )
    

    But, in fact PHP will (correctly) decode it as this:

    Array
        (
            [name] => Dave
            [percentage] => 0
        )
    

    This is because %10 is a valid url encoded string, and will be translated to the non-printable and in this context meaningless "Data Link Escape" character, ASCII 0x10.

    In order to get the result you expect, the request needs to be like this:

    POST /path/to/file HTTP/1.1
    Host: server.com
    User-Agent: Bob the browser/1.0
    Connection: keep-alive
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 27
    
    name=Dave&percentage=%25100
    

    So the value you actually send in the POST body is %25100. This will be correctly decoded as %100.

提交回复
热议问题