Php – Is it safe to directly access superglobals with isset()

PHP

I understand that one should always try to avoid direct access to superglobals to put values from superglobals into variables, eg.:

$name = filter_input(INPUT_POST, 'name'); 
//instead of 
$name = $_POST['name']; // not safe

My question if it is safe to use isset() to test if a superglobal is present, eg:

if (isset($_POST['name')) {

}

I personally do not understand why this would not be good practice, but I am not that advanced in programming.

(Recently I started using Netbeans, and it gives me a tooltip recommending to change this code. Maybe the message is not entirely correct, but I would rather know for sure. I have not found a good answer anywhere else.)

Best Answer

It is safe to use isset function in super globals. It is very effective to trigger events for a specific submit event. It only checks if the global variable is existing and will not trigger any damage such as XSS/Code/SQL injection.

I think the recommendation you received to change the code is that you need to...

$name = filter_input(INPUT_POST, 'name'); 

then...

if (isset($name)) {
}
Related Topic