Home >Backend Development >PHP Tutorial >PHP POST method_PHP tutorial

PHP POST method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:171335browse

Compare the differences between form POST and fsockopen submission methods.

When the form is submitted via POST

$_POST and php://input can get the value, $HTTP_RAW_POST_DATA is empty
$_POST organizes the submitted data in an associative array, and performs encoding processing on it, such as urldecode, and even encoding conversion.
php://input can obtain unprocessed POST raw data through file reading through the input stream

php://input allows reading the raw data of POST. It puts less pressure on memory than $HTTP_RAW_POST_DATA and does not require any special php.ini settings. php://input cannot be used with enctype="multipart/form-data".

fsockopen submits POST data
Example:

$sock = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$sock) die("$errstr ($errno) ");
$data = "txt=" . urlencode("中") . "&bar=" . urlencode("Value for Bar");
fwrite($sock, "POST /posttest/response. php HTTP/1.0 ");
fwrite($sock, "Host: localhost ");
fwrite($sock, "Content-type: application/x-www-form-urlencoded ");
fwrite($sock, "Content-length: " . strlen($data) . " ");
fwrite($sock, "Accept: */* ");
fwrite($sock, " ");
fwrite($sock, "$data ");
fwrite($sock, " ");
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str ";
echo " ";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
echo $body;

Consistent with the result (1)

Conclusion:
1. Use php://input to easily get the original POST data

2. $HTTP_RAW_POST_DATA is only valid when the Content-Type type of POST is not recognized by PHP

POST data usually submitted through page forms cannot be extracted through $HTTP_RAW_POST_DATA. Its encoding type attribute (enctype attribute) is application/x-www-form-urlencoded, multipart/form-data.

Note: Even if you explicitly change the enctype attribute in the page to a type that is not recognized by PHP, it will still be invalid.
Since the form submission encoding attribute is form-limited, unrecognizable types will be considered to be submitted in the default encoding (i.e. application/x-www-form-urlencoded)

3. $_POST can only be obtained when the data is submitted by application/x-www-form-urlencoded type.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508353.htmlTechArticleCompare the differences between form POST and fsockopen submission. When the form is submitted in POST mode, $_POST and php://input can get the value, and $HTTP_RAW_POST_DATA is empty. $_POST uses the associated number...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn