PHP头条
热点:

介绍几种PHP获取POST数据技巧


对于一个经验丰富的PHP老手来说,他可以灵活方便的运用PHP语言实现很多他所能想到的功能。从这一点也能看出,PHP是一个功能强大的语言。下面我们来一起看看PHP获取POST数据的几种方法。

  • PHP垃圾回收机制防止内存溢出
  • 技巧分享 PHP性能优化
  • PHP函数isset()只能用于变量
  • 迅速学会PHP加密解密技巧
  • PHP函数fwrite安全用于二进制文件

一)表单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.0\r\n");  
  5. fwrite($sock, "Host: localhost\r\n");  
  6. fwrite($sock, "Content-type: applicat
    ion/x-www-form-urlencoded\r\n");  
  7. fwrite($sock, "Content-length: " . 
    strlen($data) . "\r\n");  
  8. fwrite($sock, "Accept: */*\r\n");  
  9. fwrite($sock, "\r\n");  
  10. fwrite($sock, "$data\r\n");  
  11. fwrite($sock, "\r\n");  
  12. $headers = "";  
  13. while ($str = trim(fgets($sock,
     4096)))  
  14. $headers ."$str\n";  
  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数据。

www.phpzy.comtrue/php/11539.htmlTechArticle介绍几种PHP获取POST数据技巧 对于一个经验丰富的PHP老手来说,他可以灵活方便的运用PHP语言实现很多他所能想到的功能。从这一点也能看出,PHP是一个功能强大的语言。下面我们来一...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐