php页面验证密码访问

单文件简单版,本站还有另一个版本供参考:网站任意页面添加访问密码

<?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 "验证成功";
© 版权声明
THE END
如果喜欢,可以【点赞】【分享】【收藏】
点赞8赞赏 分享
评论 抢沙发
头像
人生坎坎,山山而峦,不过尔尔;心路漫漫,水水而川,如此悠悠
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容