Introduction to several php post Data Acquisition Techniques

Source: Internet
Author: User
Tags form post

For an experienced

(1) PHP obtains POST data when the form POST method is submitted

$ _ POST and php: // input can get the value. $ HTTP_RAW_POST_DATA is empty.
$ _ 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.

(2) fsockopen submits POST data and PHP obtains POST data

  1. $ Sock = fsockopen ("localhost", 80,
    $ Errno, $ errstr, 30 );
  2. If (! $ Sock) die ("$ errstr ($ errno) n ");
  3. $ Data = "txt =". urlencode ("medium ").
    "& Bar =". urlencode ("Value for Bar ");
  4. Fwrite ($ sock, "POST/posttest/response
    . Php HTTP/1.0rn ");
  5. Fwrite ($ sock, "Host: localhostrn ");
  6. Fwrite ($ sock, "Content-type: applicat
    Ion/x-www-form-urlencodedrn ");
  7. Fwrite ($ sock, "Content-length :".
    Strlen ($ data). "rn ");
  8. Fwrite ($ sock, "Accept: */* rn ");
  9. Fwrite ($ sock, "rn ");
  10. Fwrite ($ sock, "$ datarn ");
  11. Fwrite ($ sock, "rn ");
  12. $ Headers = "";
  13. While ($ str = trim (fgets ($ sock,
    4096 )))
  14. $ Headers. = "$ strn ";
  15. Echo "n ";
  16. $ Body = "";
  17. While (! Feof ($ sock ))
  18. $ Body. = fgets ($ sock, 4096 );
  19. Fclose ($ sock );
  20. Echo $ body;

PHP obtains POST data. 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: PHP can obtain POST data only when data is submitted by application/x-www-form-urlencoded type.


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.