-3

I am getting undefined index error after logging out from my web page. When i stay loged in it is all good, but when there is no1 loged in it shows error. Session active i checked. Undefined index: User from

if($_SESSION['Auth']['User']['role'] == 'admin') {
    echo 'some html code';
}

only when no user is loged in! Any ideas? Thanks in advance.

3 Answers3

2

If no one is logged in, then $_SESSION['Auth']['User']['role'] does not exist, so you are getting the undefined index error. Try this instead:

if (isset($_SESSION['Auth']['User']['role']) 
    && $_SESSION['Auth']['User']['role'] == 'admin') { 
    // admin is logged in
}
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
0

you have destroy the session when logout. so $_SESSION['Auth']['User']['role'] on login page.

But you can avoid the error which you are getting by checking isset

if(isset($_SESSION['Auth']['User']['role']) && $_SESSION['Auth']['User']['role'] == 'admin') {
    echo 'some html code';
}
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
-1

Try this

if(isset($_SESSION['Auth']['User']['role']))
{ 
        $_SESSION['Auth']['User']['role'] == 'admin') 
        {
            echo 'some html code';
        }
}
AeJey
  • 1,447
  • 20
  • 40