Overcoming Warning in NetBeans - PHP - Do not use Superglobals Array directly $_GET

5.1k views Asked by At

I have some lines of code like this;

A.

    if ( isset($_GET["logout"]) && $_GET["logout"] == 1){
    do_something;
    }

I replace with ( to get rid of the warning) ;

B.

    if ( isset( filter_input(INPUT_GET,'logout',FILTER_SANITIZE_NUMBER_INT)  ) &&  
    filter_input(INPUT_GET,'logout',FILTER_SANITIZE_NUMBER_INT) == 1){ 
    do_something;
    }

but i end up breaking the application with B. The login screen does not even appear. No error in the apache error log as well.

This is the sample url;

 http://192.168.0.91/test-app/?logout=1

What is wrong with my B. syntax/code? I guess i am using filter_input wrongly?

Update1:

php version ->php5-5.4.20-34.3.x86_64 netbeans version -> 8.0.1

2

There are 2 answers

0
MarcoZen On

Code B. was fixed by changing to ;

if ( filter_input(INPUT_GET,"logout",FILTER_SANITIZE_STRING) !== null &&    
filter_input(INPUT_GET,"logout",FILTER_SANITIZE_INT)==1) { 
dosomething;
}

Application runs fine now.

The inspiration for this fix is a fantastic article at http://kunststube.net/isset/ by David Zentgraf. Wish there was a way to contact him to thank him but ... apparently he is a regular here.

Thanx Bro :)

1
rjdown On
  1. There is nothing wrong with A. Disable the hint in Netbeans, or ignore it.

  2. No, there is nothing wrong with B. filter_input returns null if the key does not exist, and isset will return false if the condition is null, so your logic is fine and the usage of the function is too. The only problem might be that your PHP version is too old. filter_input was introduced in 5.2.0.