I've got this message warning on Netbeans 7.4 for PHP while I'm using $_POST, $_GET, $_SERVER, ....
Do not Access Superglobal $_POST Array Directly
What does it mean? What can I do to correct this warning?
Edit: The Event sample code still shows this warning.
filter_input(INPUT_POST, 'var_name')
instead of $_POST['var_name']
filter_input_array(INPUT_POST)
instead of $_POST
Although a bit late, I've come across this question while searching the solution for the same problem, so I hope it can be of any help...
Found myself in the same darkness than you. Just found this article, which explains some new hints introduced in NetBeans 7.4, including this one:
https://blogs.oracle.com/netbeansphp/entry/improve_your_code_with_new
The reason why it has been added is because superglobals usually are filled with user input, which shouldn't ever be blindly trusted. Instead, some kind of filtering should be done, and that's what the hint suggests. Filter the superglobal value in case it has some poisoned content.
For instance, where I had:
$_SERVER['SERVER_NAME']
I've put instead:
filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING)
You have the filter_input and filters doc here:
I agree with the other answerers that in most cases (almost always) it is necessary to sanitize Your input.
But consider such code (it is for a REST controller):
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
return $this->doGet($request, $object);
case 'POST':
return $this->doPost($request, $object);
case 'PUT':
return $this->doPut($request, $object);
case 'DELETE':
return $this->doDelete($request, $object);
default:
return $this->onBadRequest();
}
It would not be very useful to apply sanitizing here (although it would not break anything, either).
So, follow recommendations, but not blindly - rather understand why they are for :)
Just use
filter_input(INPUT_METHOD_NAME, 'var_name') instead of $_INPUT_METHOD_NAME['var_name'] filter_input_array(INPUT_METHOD_NAME) instead of $_INPUT_METHOD_NAME
e.g
$host= filter_input(INPUT_SERVER, 'HTTP_HOST');
echo $host;
instead of
$host= $_SERVER['HTTP_HOST'];
echo $host;
And use
var_dump(filter_input_array(INPUT_SERVER));
instead of
var_dump($_SERVER);
N.B: Apply to all other Super Global variable
来源:https://stackoverflow.com/questions/19767894/warning-do-not-access-superglobal-post-array-directly-on-netbeans-7-4-for-ph