Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 840
Renato Probst Avatar asked Sep 25 '13 17:09

Renato Probst


2 Answers

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.

like image 101
BlitZ Avatar answered Nov 15 '22 15:11

BlitZ


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
}
like image 21
Amal Murali Avatar answered Nov 15 '22 17:11

Amal Murali