How to check if php://input is set?

半世苍凉 提交于 2019-12-09 14:40:49

问题


I need to check if php://input exists/isset. Does it work with php isset() ? What is the proper way to check it?


回答1:


Try to test it with file_get_contents() (for reading) + empty() or boolean conversion (for testing):

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

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

From php.net:

Note: Prior to PHP 5.6, a stream opened with php://input could only be read once; 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. 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.




回答2:


You can get the contents of php://input using file_get_contents and check the return value to see if it's actually set:

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



回答3:


it returns true if variable exist and its not null

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

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

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



回答4:


Suppose you are getting user input from a POST request you can check if it's a set like this

 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
      }
 }


来源:https://stackoverflow.com/questions/19011711/how-to-check-if-php-input-is-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!