Check if a filled-in session exists

Asked

Viewed 6,009 times

3

I’m having trouble checking if there’s anything within the session. The functions I’m using are: Function to check if you have someone logged in

if(usuarioestalogado()){?>
<p class="alert-success">Você está logado como: <?= usuariologado() ?></p>
else...

Ancillary functions

 session_start();
function logausuario($email){
  $_SESSION["usuario_logado"] = $email;
}
function usuarioestalogado()
{
return (isset($_SESSION["usuario_logado"]));
 }

Entertaining he never enters the if only in the Else and by looking at cookies, Siession is present at: inserir a descrição da imagem aqui

Does anyone have any idea why this occurs?

  • These functions are in a different if file?

  • var_dump($_SESSION) returns what?

  • @rray are yes.

  • @Andréribeiro returns array(1) { ["usuario_logado"]=> NULL } array(1) { ["usuario_logado"]=> NULL }

  • @Rodolfooliveira Very strange. Take another test: var_dump($_SESSION, isset($_SESSION["usuario_logado"]))

  • Got to show a little more code?

Show 1 more comment

1 answer

3

If I am not mistaken in comparisons PHP considers Null as False. One of the alternatives is to do the following:

function usuarioestalogado()
{
return isset($_SESSION["usuario_logado"])) && !empty($_SESSION["usuario_logado"])  ? true : false;
 }

Browser other questions tagged

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