一个php页面访问加密,可以加在很多php文件前面

有的时候某些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.wmxxxx.com/blog/time/auth.css">
</head>
<body>
<div class="adcenter"></div>
    <div class="lowin">
        <div class="lowin-brand">
            <img src="https://cdn.wmxxxx.com/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体验地址

Last modification:July 6th, 2019 at 05:00 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment

简爱博客