1

I have probably made a very simple mistake with this, but I do not see how.. If any of you could help me, I would appreciate it lots.

This is my error:
Notice: Undefined index: admin in C:\xampp\htdocs\forums\classes\User.php on line 75

Here is User.php

public function hasPermission($key) {
    $group = $this->_db->get('groups', array('id', '=', $this->data()->group));

    if ($group->count()) {
        $permissions = json_decode($group->first()->permissions, true);

        if ($permissions[$key] == true) { <<< This is line 75 <<<
            return true;
        }
    }
    return false;
}

Here is where I use hasPermission()

if($user->hasPermission("admin")){
    echo "You are an administrator";
}

var_dump($key);:

string(5) "admin"

var_dump($permissions);

array(1) { ["admin"]=> int(1) }

var_dump($permissions[$key]); outputs:

NULL
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Kondax Design
  • 175
  • 1
  • 1
  • 15

1 Answers1

1

Try to use this one:

if (isset($permissions[$key]) && $permissions[$key] == 1) {
   return true;
} else {
   return false;
}
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • If there's a bug, you should fix it, not hide it. Working around using `isset()` might work now, but just keep in mind that hidden bugs often come back to bite you. – Amal Murali Dec 31 '13 at 13:01