Index undefined when it shifts

1

I ran a test and I put a way to identify when the user is logged into the site, and this is working well. But when I uncheck and go to the index, which is the page that I put the code, the error appears

  

Notice: Undefined index: level in C: \ Program Files (x86) \ EasyPHP-DevServer-14.1VC9 \ data \ localweb \ site tcc \ index.php on line 5

I wanted to know a way to get this out of the way, and only the message I posted to users who logged in would appear. Code I've used

<?php
session_start();
include "php/conexao.php";

if($_SESSION['nivel'] == "admin" ||  $_SESSION['nivel']== "usuario"){ 
echo "oi logado";
}
?>
    
asked by anonymous 02.11.2018 / 19:22

1 answer

0

You can use isset () to check if the 'level' index exists in the session. That way it will not generate an error if it does not exist.     

if(isset($_SESSION['nivel'])){ 
  echo "oi logado";
}
?>
    
02.11.2018 / 19:46