Simple PHP Login not working in Kirby 3?

I’m converting a site over to Kirby 3, the site uses a basic PHP login with 1 username and password.

(u: test p: test).

session_start();

$userinfo = array(
    'test'=>'098f6bcd4621d373cade4e832627b4f6'
);

$msg = "";

if(isset($_POST['username'])) {

    if($userinfo[$_POST['username']] === md5($_POST['password'])) {
        $_SESSION['username'] = true;
    }
    else {
        $msg = "<h3>SORRY, YOUR USERNAME OR PASSWORD ARE INCORRECT.</h3>";
    }
}

When the user enters the correctly password they are logged in and can see additional navigation, this is all working fine.

The problem I’m having is, if a user enters the wrong username or password. On the non-Kirby site the $msg is echoed under the form, but for some reason on the Kirby site it is bringing up the debug mode and showing the error “Undefined index” instead of showing the $msg???

Whoops\Exception\ErrorException 

 if($userinfo[$_POST['username']] === md5($_POST['password'])) {

The PHP is in the head snippet and the form is on the footer snippet.

If I switch debug mode off the site then goes to a broken page “This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.

Any help would be great.

If the user enters a wrong username, $userinfo[$_POST['username']] does not exists, hence you need to check if the key exists in the array:

 if($userinfo[$_POST['username']] ?? ''  === md5($_POST['password'])) {

Here is how to restrict access in Kirby: Restricting access to your site | Kirby CMS

Thank you very much!