简体   繁体   English

如何检查是否设置了php://输入?

[英]How to check if php://input is set?

I need to check if php://input exists/isset. 我需要检查php://input存在/ isset。 Does it work with php isset() ? 它是否适用于php isset() What is the proper way to check it? 检查它的正确方法是什么?

Try to test it with file_get_contents() (for reading) + empty() or boolean conversion (for testing): 尝试使用file_get_contents() (用于读取)+ empty()布尔转换 (用于测试)来测试它:

<?php
$input = file_get_contents('php://input');

if ($input) {
   // exists
} else {
   // not exists
}

From php.net : 来自php.net

Note: Prior to PHP 5.6 , a stream opened with php://input could only be read once; 注意: 在PHP 5.6之前 ,使用php://input打开的流只能读取一次; the stream did not support seek operations. 流不支持搜索操作。 However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. 但是,根据SAPI实现,可能会打开另一个php://输入流并重新开始读取。 This is only possible if the request body data has been saved. 只有在保存了请求正文数据后才能执行此操作。 Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND. 通常,这是POST请求的情况,但不是其他请求方法,例如PUT或PROPFIND。

You can get the contents of php://input using file_get_contents and check the return value to see if it's actually set: 您可以使用file_get_contents获取php://input的内容并检查返回值以查看它是否实际设置:

$input = file_get_contents("php://input");
if ($input) {    
    // set     
}
else {
   // not set
}

it returns true if variable exist and its not null 如果变量存在且它不为null,则返回true

$foo = 'bar';
var_dump(isset($foo));        -> true

$baz = null;
var_dump(isset($baz));        -> false

var_dump(isset($undefined));  -> false

Suppose you are getting user input from a POST request you can check if it's a set like this 假设您从POST请求获得用户输入,您可以检查它是否是这样的集合

 if(isset($_POST['var_name']))
 {
      //for additional checking  like if  it's not  empty do this
      if(!empty($_POST['var_name']))
      {
           //Do whatever you want here
      }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM