PHP fsockopen submit POST data explanation

Source: Internet
Author: User
Tags form post

Compare two different methods of form [url = javascript:;] POST [/url] And fsockopen commit.

When the form POST method is submitted
$ _ POST and [url = javascript:;] php [/url]: // input can get the value, $ HTTP_RAW_POST_DATA is blank
$ _ POST organizes submitted data in an associated array and performs encoding, such as urldecode or even encoding conversion.
Php: // input: Raw POST data that has not been processed can be obtained by reading files from the input stream.

Php: // input allows reading original POST data. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings. Php: // input cannot be used for enctype = "multipart/form-data ".

Fsockopen submits POST Data
Example:
$ Sock = fsockopen ("localhost", 80, $ errno, $ errstr, 30 );
If (! $ Sock) die ("$ errstr ($ errno )");
$ Data = "txt =". urlencode ("medium"). "& bar =". urlencode ("Value for Bar ");
Fwrite ($ sock, "POST/posttest/resp ***** e. 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 (1) Results

Conclusion:
1. You can use php: // input to conveniently retrieve the original POST data.

2. $ HTTP_RAW_POST_DATA is valid only when the POST Content-Type is not recognized by PHP.
For example, the POST data submitted through the page form cannot be extracted through $ HTTP_RAW_POST_DATA. The encoding type attribute (enctype attribute) is application/x-www-form-urlencoded and multipart/form-data.
Note: even if the enctype attribute is explicitly changed to an unrecognized PHP type on the page, it still does not work.
Because the form submission encoding attribute is a form limitation, unrecognized types are considered to be submitted in the default encoding mode (application/x-www-form-urlencoded)

3. $ _ POST is only available when data is submitted by application/x-www-form-urlencoded type.
With -.shtml, search is regarded as static page pulling.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.