Error in isset PHP

4

It's giving you an error when I use isset with new PHP methods:

<?php
require './fb.php';
if (isset(filter_input(INPUT_SESSION, "fb_access_token")) && !empty(filter_input(INPUT_SESSION, "fb_access_token"))):
    echo "Ta logado!";
else:
    header("Location: login.php");
endif;
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in C:\wamp64\www\bw7\index.php on line 9

Already using global variables, it works:

<?php
require './fb.php';
if(isset($_SESSION["fb_access_token"])):
    echo "Ta logado!";
else:
    header("Location: login.php");
endif;

Why?

    
asked by anonymous 16.10.2017 / 18:58

2 answers

4

isset() was not meant to be used with expressions, the documentation says what the purpose is.

  

Determine if a variable is set and is not NULL.

Translation:

  

Determined if the variable is set and is not null.

It does not make sense to code like

isset(count(false));

empty() from php5.5 supports expressions so in this case is most appropriate. If you want to check if there is something in the session go straight to the point with:

if(!empty($_SESSION["fb_access_token"])){
   echo 'logado';
}else{
    header("Location: login.php");
}

Relationships:

Can I use empty and isset in a variable?

When should I use empty or isset?

    
16.10.2017 / 19:30
3

It's because of the expression, try to do it this way:

<?php
require './fb.php';
if (isset(filter_var("fb_access_token")) && !empty(filter_var("fb_access_token"))):
    echo "Ta logado!";
else:
    header("Location: login.php");
endif;
    
16.10.2017 / 19:03