zoukankan      html  css  js  c++  java
  • PHP之fsockopen提交POST数据讲解

    对比表单[url=javascript:;]POST[/url]和fsockopen提交两种不同方式的区别。

    表单POST方式提交情况下
    $_POST 与 [url=javascript:;]php[/url]://input可以取到值,$HTTP_RAW_POST_DATA 为空
    $_POST 以关联数组方式组织提交的数据,并对此进行编码处理,如urldecode,甚至编码转换。
    php://input 可通过输入流以文件读取方式取得未经处理的POST原始数据

    php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。php://input 不能用于 enctype="multipart/form-data"。

    fsockopen提交POST数据
    例:
    $sock = fsockopen("localhost", 80, $errno, $errstr, 30);
    if (!$sock) die("$errstr ($errno) ");
    $data = "txt=" . urlencode("中") . "&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;
    与(一)结果一致

    结论:
    1. 用php://input可以很便捷的取到原始POST数据

    2. $HTTP_RAW_POST_DATA 仅在POST的Content-Type类型不为PHP识别时才有效
    如通常通过页面表单提交后的POST数据,不能通过$HTTP_RAW_POST_DATA提取到。因其编码类型属性(enctype属性)为 application/x-www-form-urlencoded、multipart/form-data。
    注:即使在页面内显性地改变enctype属性为PHP不可识别的类型,仍无效。
    因表单提交编码属性是表单限定,不可识别的类型将被认为按默认编码方式提交(即application/x-www-form-urlencoded)

    3. $_POST仅当数据按 application/x-www-form-urlencoded 类型提交时才能得到。
    加上 -.shtml就搜索就认为是静态页面拉。
  • 相关阅读:
    WSL中使用npm install报错
    在npm install时node-gyp出现错误
    Chrome禁用隐藏www和m
    Git始终忽略特定文件的某一行内容
    macOS关闭修改扩展名的提示
    解决安装Anaconda后ZSH中使用的依然是系统自带的Python
    macOS上更顺手的终端
    自用的越狱插件
    Tomcat安装后修改路径方法
    TestStack.White安装详解
  • 原文地址:https://www.cnblogs.com/lemon66/p/4102729.html
Copyright © 2011-2022 走看看