介紹幾種PHP擷取POST資料技巧_PHP教程

來源:互聯網
上載者:User
對於一個經驗豐富的

(一)表單POST方式提交情況下PHP擷取POST資料

$_POST 與 php://input可以取到值,$HTTP_RAW_POST_DATA 為空白
$_POST 以關聯陣列方式組織提交的資料,並對此進行編碼處理,如urldecode,甚至編碼轉換。
php://input 可通過輸入資料流以檔案讀取方式取得未經處理的POST未經處理資料

(二)fsockopen提交POST資料下PHP擷取POST資料

  1. $sock = fsockopen("localhost", 80,
    $errno, $errstr, 30);
  2. if (!$sock) die("$errstr ($errno)n");
  3. $data = "txt=" . urlencode("中") .
    "&
    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擷取POST資料結論:

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 類型提交時才能實現PHP擷取POST資料。


http://www.bkjia.com/PHPjc/445978.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445978.htmlTechArticle對於一個經驗豐富的 (一)表單POST方式提交情況下PHP擷取POST資料 $_POST 與 php://input可以取到值,$HTTP_RAW_POST_DATA 為空白 $_POST 以關聯陣列方式...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.