有的时候某些php文件不想让别人访问,想要加个访问权限控制,输入密码才可以访问

这里分享一个自己写的php访问时输入密码才可以访问的demo,欢迎提意见和bug

哪里想加密码,就加哪里

<?php 
$pwd = '请输入要设置的密码';
$html = <<<STR
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>时光机</title>
    <link rel="stylesheet" href="https://cdn.nikm.cn/blog/time/auth.css">
</head>
<body>
<div class="adcenter"></div>
    <div class="lowin">
        <div class="lowin-brand">
            <img src="https://cdn.nikm.cn/blog/time/kodinger.jpg" alt="logo">
        </div>
        <div class="lowin-wrapper">
            <div class="lowin-box lowin-login">
                <div class="lowin-box-inner">
                </div>
            </div>
            <div class="lowin-box lowin-register">
                <div class="lowin-box-inner">
                    <form action="" method="post" accept-charset="utf-8">
                        <p>哎呀,主人来看我咯</p>
                        <div class="lowin-group">
                            <label>请输入访问密码</label>
                            <input type="password" name="pwd" class="lowin-input">
                        </div>
                        <button class="lowin-btn">
                            Sign Up
                        </button>
                        <div class="text-foot">
                            By:<a href="https://blog.wmxxxx.com/archives/12.html" class="login-link">吾梦一梦</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
STR;



if (empty($_POST)) {
     if (isset($_COOKIE['pwd'])) {
        $pwd_md5 = md5($pwd);
        if ($_COOKIE['pwd']!=$pwd_md5) {
            echo $html;
            exit();
        }
    }else{
        echo $html;
        exit();
    }
}else{
    if ($_POST['pwd'] == $pwd) {
        $pwd_md5 = md5($pwd);
        setcookie('pwd',$pwd_md5);
    }else{
        echo '<script>alert("密码错误");</script>'.$html;
        exit();
    }
    
}
   
 ?>

demo体验地址

最后修改:2019 年 07 月 06 日 05 : 00 PM
如果觉得我的文章对你有用,请随意赞赏