Session variable does not exist

Asked

Viewed 55 times

0

A few days ago I reported a problem that was happening in the code I was using, but as it lacked many things I decided to use another. In the new code, there are variables to save the session, as below:

if (mysql_num_rows($query) != 1) {
  // Mensagem de erro quando os dados são inválidos e/ou o usuário não foi encontrado
  echo "Login inválido!"; exit;
} else {
  // Salva os dados encontrados na variável $resultado
  $resultado = mysql_fetch_assoc($query);

  // Se a sessão não existir, inicia uma
  if (!isset($_SESSION)) session_start();

  // Salva os dados encontrados na sessão
  $_SESSION['UsuarioID'] = $resultado['id'];
  $_SESSION['UsuarioNome'] = $resultado['nome'];
  $_SESSION['UsuarioNivel'] = $resultado['nivel'];

  // Redireciona o visitante
  header("Location: restrito.php"); exit;
}

Already the code of the restriction page, there are the following codes:

if (mysql_num_rows($query) != 1) {
  // Mensagem de erro quando os dados são inválidos e/ou o usuário não foi encontrado
  echo "Login inválido!"; exit;
 } else {
  // Salva os dados encontrados na variável $resultado
  $resultado = mysql_fetch_assoc($query);

  // Se a sessão não existir, inicia uma
  if (!isset($_SESSION)) session_start();

  // Salva os dados encontrados na sessão
  $_SESSION['UsuarioID'] = $resultado['id'];
  $_SESSION['UsuarioNome'] = $resultado['nome'];
  $_SESSION['UsuarioNivel'] = $resultado['nivel'];

  // Redireciona o visitante
  header("Location: restrito.php"); exit;
}

However when entering user and password as requested on the test page I did to run the codes, the following message appears:

Notice: Undefined index: Usuarionome in C: Program Files (x86) Easyphp-Devserver-14.1VC9 data localweb sistema restrito.php on line 28

I tried to change "Usuarioid", "Usuarionome" and "Usuarionivel" to the name of the attributes of the table (id, name and level, respectively) but I am redirected to the index and nothing happens.

  • $_SESSION is a superglobal then, theoretically, it will always be set. It makes no sense to use isset in it. It would only give session_start without even if

3 answers

1

  • In case the line "if (!isset($_SESSION)) session_start();" is the login, no?

1

$_SESSION is a superglobal then, theoretically, it will always be set. It makes no sense to use isset in it because it would always return true.

Then your if:

if (!isset($_SESSION)) session_start();

will always return false and the session will never start.

The resolution would just be session_start without even if:

session_start();

For in the documentation says:

session_start() creates a session or summarizes the current session based on a session id passed via GET or POST, or passed via cookie.

0

if (!isset($_SESSION)) {
    session_start();
    $_SESSION['UsuarioID'] = $resultado['id'];
    ...
}

Try to use your if this way

Browser other questions tagged

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