php提供了简单的认证功能,我们可以通过header来调用该功能

<?php

if ($_SERVER['PHP_AUTH_USER'] == 'test' && $_SERVER['PHP_AUTH_PW'] == 'test') {
    echo "通过认证啦" . "<br>";
    echo "username: " . $_SERVER['PHP_AUTH_USER'] . "<br>";
    echo "password: " . $_SERVER['PHP_AUTH_PW'];
} else {
    Header("WWW-Authenticate: Basic realm='USER AUTH'");

    Header("HTTP/1.0 401 Unauthorized");
}
?>