Error in isset PHP

Asked

Viewed 362 times

4

Error when using 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?

  • Can’t use isset() in expressions, you can switch to empty() if the php version is 5.5 or higher.

  • But filter_input(INPUT_SESSION, "fb_access_token") does not do the same thing as $_SESSION["fb_access_token"])?

  • 1

    I just saw that INPUT_SESSION has not been implemented yet.

2 answers

4


isset() 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 void.

Doesn’t make sense a code like

isset(count(false));

empty() from php5.5 supports expressions so in this case it is the 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");
}

Related:

I can use Empty and isset in a variable?

When should I use Empty or isset?

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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.