Empty $ _POST without Content-type

If I make a POST request where the content type is not set in the request header, the $ _POST variable remains empty.

Question: How to get PHP to populate $ _POST?

I ran into the problem of creating a Javascript AJAX request using XDomainRequest , where you cannot define headers. To make it easier for you to understand the problem, you can simulate the same effect without Javascript in PHP like this:

$data = 'test=1'; $fp = fsockopen('www.yourpage.org', 80, $errno, $errstr, 5); fputs($fp, "POST /test_out.php HTTP/1.1\r\n"); fputs($fp, "Host: www.yourpage.org\r\n"); //fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); while(!feof($fp)) { echo fgets($fp, 128); } fclose($fp); 

test_out.php will be

 var_dump($_POST); 

Without the right content type, the variables in $ _POST magically disappear.

This is a more educational question. I ask about this because most people here cannot understand that this type of content has this effect. I was asked to "come back with the facts." So you go.

+4
source share
1 answer

You can override / insert the required header using Apache mod_headers if you need to. Or else resort to manual recovery of the $ _POST array. (See also userland multipart / form-data handler )

If it is always urlencoded, just read from php://input (which contains the body of the POST request) and use parse_str :

 parse_str(file_get_contents("php://input"), $_POST); 

This should recreate the POST array, pretty much like PHP.

+7
source

Source: https://habr.com/ru/post/1382296/


All Articles