Home » Php » php – Cannot use isset () on the result of a function call (you can use "null != Func ()" instead)

php – Cannot use isset () on the result of a function call (you can use "null != Func ()" instead)

Posted by: admin July 12, 2020 Leave a comment

Questions:
( ! ) Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in C:\Users\sanoj\Documents\NetBeansProjects\video-site\app\detect.php on line 45
Call Stack
#   Time    Memory  Function    Location
1   0.0000  234208  {main}( )   ..\index.php:0

Previously my code was

if(!defined('WAP'))
    define('WAP', isset($_REQUEST['wap']) || isset($_REQUEST['wap2']) || isset($_REQUEST['imode'])|| isset($_REQUEST['html'])|| isset($_REQUEST['Android'])|| isset($_REQUEST['iphone'])|| isset($_REQUEST['IEMobile']));

    if (WAP)
{
    define('WIRELESS_PROTOCOL', isset($_REQUEST['wap']) ? 'wap' : (isset($_REQUEST['wap2']) ? 'wap2' : (isset($_REQUEST['iphone']) ? 'iphone' : (isset($_REQUEST['imode']) ? 'imode' : (isset($_REQUEST['IEMobile']) ? 'IEMobile' :(isset($_REQUEST['html']) ? 'html' : (isset($_REQUEST['Android']) ? 'Android' : '')))))));  

And my IDE warned Do Not Access SuperGlobal $_REQUEST Array Directly
So I used this following code

if(!defined('WAP'))
{
    define('WAP', isset(filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING)) || isset(filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING)) || isset(filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING))|| isset(filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING))|| isset(filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING))|| isset(filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING))|| isset(filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING)));
}
    if (WAP)
{
    define('WIRELESS_PROTOCOL', isset(filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING)) ? 'wap' : (isset(filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING)) ? 'wap2' : (isset(filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING)) ? 'iphone' : (isset(filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING)) ? 'imode' : (isset(filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING)) ? 'IEMobile' :(isset(filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING)) ? 'html' : (isset(filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING)) ? 'Android' : '')))))));  

Here is detect.php

<?php
$userBrowser = $_SERVER['HTTP_ACCEPT']; 
if(stristr($userBrowser, 'application/vnd.wap.xhtml+xml')) 
{
$_REQUEST['wap2'] = 1;
}

elseif(stripos($_SERVER['HTTP_USER_AGENT'],"iPod"))
{
$_REQUEST['iphone'] = 1;

}
elseif(stripos($_SERVER['HTTP_USER_AGENT'],"iPhone"))
{
$_REQUEST['iphone'] = 1;

}
elseif(stripos($_SERVER['HTTP_USER_AGENT'],"Android"))
{
$_REQUEST['Android'] = 1;

}
elseif(stripos($_SERVER['HTTP_USER_AGENT'],"IEMobile"))
{
$_REQUEST['IEMobile'] = 1;

}
elseif(stristr($userBrowser, 'DoCoMo/' || 'portalmmm/'))
{
$_REQUEST['imode'] = 1;
}

elseif(stristr($userBrowser, 'text/vnd.wap.wml')) 
{
$_REQUEST['wap'] = 1;
}
elseif(stristr($userBrowser, 'text/html')) 
{
$_REQUEST['html'] = 1;
}


if(!defined('WAP'))
    define('WAP', isset($_REQUEST['wap']) || isset($_REQUEST['wap2']) || isset($_REQUEST['imode'])|| isset($_REQUEST['html'])|| isset($_REQUEST['Android'])|| isset($_REQUEST['iphone'])|| isset($_REQUEST['IEMobile']));

    if (WAP)
{
    define('WIRELESS_PROTOCOL', isset($_REQUEST['wap']) ? 'wap' : (isset($_REQUEST['wap2']) ? 'wap2' : (isset($_REQUEST['iphone']) ? 'iphone' : (isset($_REQUEST['imode']) ? 'imode' : (isset($_REQUEST['IEMobile']) ? 'IEMobile' :(isset($_REQUEST['html']) ? 'html' : (isset($_REQUEST['Android']) ? 'Android' : '')))))));  

if (WIRELESS_PROTOCOL == 'wap')
      {
$browser_t = "mobile";
      }
elseif (WIRELESS_PROTOCOL == 'wap2')
      {


$browser_t = "mobile";


      }
elseif (WIRELESS_PROTOCOL == 'imode')
      {

$browser_t = "mobile";

      }
      elseif (WIRELESS_PROTOCOL == 'iphone')
      {


$browser_t = "smartphone";

      }
      elseif (WIRELESS_PROTOCOL == 'Android')
      {


$browser_t = "smartphone";

      }
       elseif (WIRELESS_PROTOCOL == 'IEMobile')
      {

$browser_t = "smartphone";

      }
      elseif (WIRELESS_PROTOCOL == 'html')
      {

     $mobile_browser = '0';

if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
    strtolower($_SERVER['HTTP_USER_AGENT']))){
    $mobile_browser++;
    }

if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or 
    ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
    $mobile_browser++;
    }

$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda','xda-');

if(in_array($mobile_ua,$mobile_agents)){
    $mobile_browser++;
    }
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
    $mobile_browser++;
    }
        if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'iemobile')>0) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
    $mobile_browser=0;
    }


if($mobile_browser>0){
   // do something wap

$browser_t = "mobile";

}
// non-mobile
else
{

$_SESSION['Browser_d'] = "web";
$browser_t = "web";

}
   } else {
   // do something else html

$_SESSION['Browser_d'] = "web";
$browser_t = "web";
   }

      }


    else
    {
$mobile_browser = '0';

if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
    strtolower($_SERVER['HTTP_USER_AGENT']))){
    $mobile_browser++;
    }

if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or 
    ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
    $mobile_browser++;
    }

$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda','xda-');

if(in_array($mobile_ua,$mobile_agents)){
    $mobile_browser++;
    }
    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'iemobile')>0) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
    $mobile_browser++;
    }
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
    $mobile_browser=0;
    }


if($mobile_browser>0){
   // do something wap

$browser_t = "mobile";

}
// non-mobile
else
{
$_SESSION['Browser_d'] = "web";
$browser_t = "web";
}

    }
     ?>

And index.php

<?php 
include "app/config.php";
include "app/detect.php";

if ($page_name=='') {
    include $browser_t.'/index.html';
    }
elseif ($page_name=='index.html') {
    include $browser_t.'/index.html';
    }
elseif ($page_name=='singlepage.html') {
    include $browser_t.'/singlepage.html';
    }
elseif ($page_name=='categories.html') {
    include $browser_t.'/categories.html';
    }
elseif ($page_name=='contact.html') {
    include $browser_t.'/contact.html';
    }
else
    {
        include $browser_t.'/404.html';
    }

?>

And config.php

<?php
$current_page_uri = $_SERVER['REQUEST_URI'];
$part_url = explode("/", $current_page_uri);
$page_name = end($part_url);
$email_id = "[email protected]";
?>

when index file is loaded it should detect device and redirect it to proper root but in my case it doesn’t
I get following error

( ! ) Notice: Undefined index: ALL_HTTP in C:\Users\sanoj\Documents\NetBeansProjects\video-site-2\app\detect.php on line 118
Call Stack
#   Time    Memory  Function    Location
1   0.0090  233992  {main}( )   ..\index.php:0
2   0.0230  295896  include( 'C:\Users\sanoj\Documents\NetBeansProjects\video-site-2\app\detect.php' )  ..\index.php:11

( ! ) Warning: include(web/404.html): failed to open stream: No such file or directory in C:\Users\sanoj\Documents\NetBeansProjects\video-site-2\index.php on line 30
Call Stack
#   Time    Memory  Function    Location
1   0.0090  233992  {main}( )   ..\index.php:0

( ! ) Warning: include(): Failed opening 'web/404.html' for inclusion (include_path='.;C:\php\pear') in C:\Users\sanoj\Documents\NetBeansProjects\video-site-2\index.php on line 30
Call Stack
#   Time    Memory  Function    Location
1   0.0090  233992  {main}( )   ..\index.php:0
How to&Answers:

Firstly, NetBeans IDE shows you those warnings so that you don’t trust the$_REQUEST values directly and validate it in some way. By checking isset($_REQUEST['wap']) you are already doing a part of checking which the IDE is suggesting. There can be other steps of this process, which you should do before you use it, like inserting to database. I ignore many of the NetBeans warnings, because, NetBeans doesn’t know, what I am going to do with my inputs / variables. It just warns me about the best practices, which may or may not be the best for my case. And warnings should be taken as suggestions only, not something you must do.

Secondly, if you use filter_input(), you don’t need to use isset() additionally. Because, filter_input() is already doing it for you. As the documentation says,

Return Values : Value of the requested variable on success, FALSE if the filter fails, or NULL if the variable_name variable is not set. If the flag FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is not set and NULL if the filter fails.

So you can just remove isset() and your code should work safely. But you may still want to check if the value was set to apply the alternative values. In that case just use empty() function, which will return FALSE when value returned from filter_input() is NULL / FALSE. So just negate the condition to match your case. Like this way –

if(!defined('WAP'))
{
    define('WAP', !empty(filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING)) || !empty(filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING)) || !empty(filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING))|| !empty(filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING))|| !empty(filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING))|| !empty(filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING))|| !empty(filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING)));
}

 if (WAP)
{
    define('WIRELESS_PROTOCOL', !empty(filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING)) ? 'wap' : (!empty(filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING)) ? 'wap2' : (!empty(filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING)) ? 'iphone' : (!empty(filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING)) ? 'imode' : (!empty(filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING)) ? 'IEMobile' :(!empty(filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING)) ? 'html' : (!empty(filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING)) ? 'Android' : '')))))));
} 

Second Error

Notice: Undefined index: ALL_HTTP

I couldn’t find any variable called ALL_HTTP in php documentation for $_SERVER. So check the link and find one which can serve your purpose. I’m not sure what you are actually trying to get with it, So I can’t suggest any.

Third Error

Warning: include(web/404.html): failed to open stream.

The error already says the file doesn’t exist. So either the file isn’t there or you have provided the path incorrectly. Pls check and fix that.

Answer:

Remove the isset() calls in your second example, and it should work.

if(!defined('WAP'))
{
    define('WAP', filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING) || filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING) || filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING)|| filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING)|| filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING)|| filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING)|| filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING));
}
if (WAP)
{
    define('WIRELESS_PROTOCOL', filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING) ? 'wap' : (filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING) ? 'wap2' : (filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING) ? 'iphone' : (filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING) ? 'imode' : (filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING) ? 'IEMobile' :(filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING) ? 'html' : (filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING) ? 'Android' : '')))))));
}

This is because the filter_input() function will return a value that evaluates to true/false already, so you do not need the isset() as well. Check out the return values in the PHP Manual for filter_input() for more info.

You may also want to check out empty() and is_null() and see how they compare with isset() in their usage. The function empty() would make more sense in this situation.

For the issue with undefined %_SERVER['ALL_HTTP'], you should use isset() there to first check if the variable exists. Example:

if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'OperaMini') > 0) {
    $mobile_browser++;
}

The error with the missing 404 file is something I cannot resolve without knowing where the file is placed. However, the include you are using is looking for:

C:\Users\sanoj\Documents\NetBeansProjects\video-site-2\web\404.html

You either need to create/move the file in that location, or change the include path to point to the right file location. You may give me the file location and I will give you the correct include path.

Answer:

You should consider using a more lisible code:

Step 1: you get the variables matching your needs in one array

$tab=array_filter(filter_input_array(INPUT_SERVER,Array(
    'wap'     =>FILTER_SANITIZE_STRING,
    'wap2'    =>FILTER_SANITIZE_STRING,
    'imode'   =>FILTER_SANITIZE_STRING,
    'html'    =>FILTER_SANITIZE_STRING,
    'Android' =>FILTER_SANITIZE_STRING,
    'iphone'  =>FILTER_SANITIZE_STRING,
    'IEMobile'=>FILTER_SANITIZE_STRING)));

Step 2: Use the first variable detected

if(!defined('WAP'))&&(count($tab)) {
   define('WAP', $tab[0]);
}

Also, as already said in other answers, you have to use filter_input() functions in order to satisfy your IDE warning Do Not Access SuperGlobal $_REQUEST Array Directly

Answer:

Use isset() as :

    if(!defined('WAP')){
      if(isset($_REQUEST['wap']) || isset($_REQUEST['wap2']) || ..){
      $wap = $_REQUEST['wap']);
      $wap2 = $_REQUEST['wap2']);
      //other variables
      define('WAP', $wap || $wap2 || ..);
    }

}

Answer:

While the first part does not directly answer your question, it is an important note and will probably save you a lot of grief in your application. You are using constants incorrectly, which can come back to bite you.

Consider the following code:

if(!defined('WAP'))
    define('WAP', isset($_REQUEST['wap']) 
    || isset($_REQUEST['wap2']) || isset($_REQUEST['imode'])
    || isset($_REQUEST['html'])|| isset($_REQUEST['Android'])
    || isset($_REQUEST['iphone'])|| isset($_REQUEST['IEMobile']));

These lines say “if WAP is not defined, then assign WAP a value of true if any of the following are in the REQUEST scope: wap, wap2, imode, html, Android, iphone, or IEMobile. If they are NOT in the request scope, assign WAP constant the value false.

I know this may feel trivial, but constants should have the same value regardless of any other system reality. Constants should be very easy to discover values for. Look at how PHP uses constants to give an idea. E_NOTICE is always the same value. Thus, you can use the integer (8) that represents E_NOTICE or you can use E_NOTICE constant to get the same exact result.

If, on the other hand, E_NOTICE was equal to 8, unless you specified that notices should trigger a fatal error, in which case E_NOTICE was equal to 16 (aka E_CORE_ERROR), then you’d have a bit of a confusing mess on your hands.

Why does this matter in this instance?

Consider your code:

if(!defined('WAP'))
{
    define('WAP', isset(filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING)) 
    || isset(filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING)) 
    || isset(filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING))
    || isset(filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING))
    || isset(filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING))
    || isset(filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING))
    || isset(filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING)));
}

You are doing a lot of mental gymnastics to ensure that a constant is set to a specific session. While there are ways to clean up this code so as not to trigger an error, it will still contain an issue which may come back to bite you, i.e. you are using constants where you should be using a variable, or, better yet, a function.

Like, the following:

function is_wap()
{
    return (filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING)
    || filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING)
    || filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING)
    || filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING)
    || filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING)
    || filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING));
}

Now, replace WAP with is_wap().

More in line with your question, you cannot use isset() against a direct function call. You can do one of three things to circumvent this:

  1. As PHP suggests, instead use null !==. This is probably the easiest shorthand solution.

Example:

if (isset(filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING)))
    ...

Can just as easily be written as

if (null !== ($imode = filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING)))
    ...

Getting you your variable assignment as well.

  1. If you insist on using isset() then you can assign the variable beforehand.

    $imode = filter_input(INPUT_REQUEST, ‘imode’, FILTER_SANITIZE_STRING);
    if (isset($imode))

  2. You can use function return values to determine the result. You can even create a custom function to handle your logic for you if a third-party or built-in function doesn’t have the right return value.

For example, if you just want to ensure that a value is not equal to null (and you’re OK with it being false), you can use this:

function isNotNull($val)
{
    return ($val !== null);
    // note that, because you've essentially passed the function result,
    // you could use this line instead:
    // return isset($val);
}

Then, in your code, you can use the custom function:

isNotNull(filter_input(INPUT_REQUEST, 'wap', FILTER_SANITIZE_STRING)) 
|| isNotNull(filter_input(INPUT_REQUEST, 'wap2', FILTER_SANITIZE_STRING)) 
|| isNotNull(filter_input(INPUT_REQUEST, 'imode', FILTER_SANITIZE_STRING))
|| isNotNull(filter_input(INPUT_REQUEST, 'html', FILTER_SANITIZE_STRING))
|| isNotNull(filter_input(INPUT_REQUEST, 'Android', FILTER_SANITIZE_STRING))
|| isNotNull(filter_input(INPUT_REQUEST, 'iphone', FILTER_SANITIZE_STRING))
|| isNotNull(filter_input(INPUT_REQUEST, 'IEMobile', FILTER_SANITIZE_STRING)));

Note that because filter_input does return false if the filter fails and null if the variable doesn’t exist. So, it may be more advantageous to omit isset entirely which will cause your logical check to determine if a variable exists without the possibility that the filter might fail.

I should note that the probability of a filter failing in production is low. Going full circle to my original point, one of the reasons it is so low is because the filters are constants, and ALWAYS represent the same thing.