php://input <> $_POST?

后端 未结 3 1414
难免孤独
难免孤独 2020-12-06 07:58

I\'m experimenting with Firefox\'s Content Security Policy. Basically it\'s a special header for the webpage that tells the browser which resources are valid.

When s

相关标签:
3条回答
  • 2020-12-06 08:51

    If a request is sent as POST it is not necessarily encoded as normal application/x-www-form-urlencoded or multipart/form-data. Should Firefox send a JSON body, then PHP doesn't know how to decode it.

    You have to check $_SERVER["HTTP_CONTENT_TYPE"]. If it contains application/json then you must indeed read php://stdin:

    if (stripos($_SERVER["HTTP_CONTENT_TYPE"], "application/json")===0) {
         $_POST = json_decode(file_get_contents("php://input"));
    // or something like that
    
    0 讨论(0)
  • 2020-12-06 08:54

    $_POST gives you form variables, which show up in the page like this:

    POST /some_path HTTP/1.1
    
    myvar=something&secondvar=somethingelse
    

    But what you're getting isn't a valid query string. It probably looks something like this:

    POST /some_path HTTP/1.1
    
    {'this':'is a JSON object','notice':'it\'s not a valid query string'}
    

    php://input gives you everything after the headers in raw form, so in this case I think it's the only way to get what you want.

    0 讨论(0)
  • 2020-12-06 08:59

    It can be several of other http request types (I am aware of 7 right now, and several place holders for more to come).
    I would print the $_REQUEST and $_SERVER to see how it actually arrives.

    0 讨论(0)
提交回复
热议问题