PHP filter_input (INPUT_SERVER, 'REQUEST_METHOD') returns null?

Why does this string return null on my real server?

 filter_input(INPUT_SERVER, 'REQUEST_METHOD'); 

Live php5.5.9 server

Did I miss something?

I thought it was used to replace the global method below?

 $_SERVER['REQUEST_METHOD']; 

some code

 public function __construct() { // Construct other generic data. $this->clientRequestMethod = filter_input(INPUT_GET, 'method'); // such as list, add, update, etc $this->clientPostMethod = filter_input(INPUT_POST, 'method'); // such as update $this->serverRequestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD'); //such as get or post } public function processEntry() { // Determine the $_SERVER['REQUEST_METHOD'] whether it is post or get. if ($this->serverRequestMethod === 'POST' && $this->clientPostMethod != null) { $this->processPost(); } else if($this->serverRequestMethod === 'GET' && $this->clientRequestMethod != null) { $this->processRequest(); } } 
+6
source share
5 answers

So the problem / error:

filter_input () does not work with INPUT_SERVER or INPUT_ENV when you use FASTCGI

The mistake has been known for years, and I did not find anything to say about it. I found several workarounds, but did not have a complete solution, so I applied the best work in this helper function to solve the whole project. To provide some level of security and avoid train debris, the function returns to filter_var (), where filter_input () fails. It uses the same format as the built-in filter_input () function for easy integration into projects and simple subsequent deletion if the error is fixed.

 function filter_input_fix ($type, $variable_name, $filter = FILTER_DEFAULT, $options = NULL ) { $checkTypes =[ INPUT_GET, INPUT_POST, INPUT_COOKIE ]; if ($options === NULL) { // No idea if this should be here or not // Maybe someone could let me know if this should be removed? $options = FILTER_NULL_ON_FAILURE; } if (in_array($type, $checkTypes) || filter_has_var($type, $variable_name)) { return filter_input($type, $variable_name, $filter, $options); } else if ($type == INPUT_SERVER && isset($_SERVER[$variable_name])) { return filter_var($_SERVER[$variable_name], $filter, $options); } else if ($type == INPUT_ENV && isset($_ENV[$variable_name])) { return filter_var($_ENV[$variable_name], $filter, $options); } else { return NULL; } } 

This seems like the best solution. Please let me know if it contains errors that may cause problems.

+6
source

I had the same problem when it worked on my local machine (OSX Mavericks, PHP version 5.4.24), and not on my real server (Cent OS 5). I upgraded the server from 5.3.9 to 5.5.15 (and added the mb and mcrypt functions, although that probably doesn't matter) and it works now.

This is probably not useful if you are on a shared host, but you can ask them if they can rebuild PHP / Apache.

+5
source

I had the same problem on my local XAMPP host, and I was madly looking for solutions. What I ended up with is a known PHP bug for this function if you use PHP in FCGI mode (FCGI / PHP 5.4 in my case). I have been verified through this link.

The workaround I used was filter_var($_SERVER['PHP_AUTH_USER'], FILTER_SANITIZE_STRING) , but this is not an alternative to filter_input . filter_input more secure.

+3
source

FastCGI seems to cause strange side effects with unexpected null values ​​when using INPUT_SERVER and INPUT_ENV with this function. You can use this code to find out if it affects your server.

If you want to be safe, the $ _SERVER and $ ENV superglobals will always work. You can use the filter * functions for Get / Post / Cookie without any problems, which is an important part!

Source: http://php.net/manual/es/function.filter-input.php#77307

+3
source

I solve this by changing my php.ini to:

 variables_order = "GPCS" 

To:

 variables_order = "GPCSE" 

By default, PHP does not register environment variables, so this change allowed them. Interestingly, the INPUT_SERVER variables INPUT_SERVER back to work again!

Just two supporting data, I am using PHP 7.0.13, and as said in other answers, this problem is due to a PHP error.

Another option is to use the following:

 filter_var(getenv('REQUEST_METHOD')); 
+2
source

Source: https://habr.com/ru/post/973624/


All Articles