PHP验证用户名与密码访问
阅读 42 作者 小林丶子
<?php
header("Content-type: text/html; charset=utf-8");
function code($user, $pass) {
      $users = ['admin'=>'123456'];
      if(isset($users[$user]) && $users[$user] === $pass) {
        return true;
      } else {
        return false;
      }
}
if(!code(@$_SERVER['PHP_AUTH_USER'], @$_SERVER['PHP_AUTH_PW'])) {
     http_response_code(401);
     header('WWW-Authenticate:Basic realm="My website"');
     echo '需要用户名和密码才能继续访问'; 
     exit;
} else {
    
}
echo "验证成功";