为任意PHP页面设置访问密码为任意PHP页面设置访问密码

生命不息,
折腾不止!

为任意PHP页面设置访问密码

本篇内容就如标题所说的,用于给任意的页面设置访问密码

使用方法

1、将下面代码保存为 MkEncrypt.php 或 下载附件

2、在要加密的页面前面引入这个 php 文件

require_once('MkEncrypt.php');

3、设置页面访问密码

MkEncrypt('页面密码');

 

MkEncrypt.php 文件内容

<?php
 
/********************************************
 * 使用方法:
 * 
 * 1、将本段代码保存为 MkEncrypt.php
 * 
 * 2、在要加密的页面前面引入这个 php 文件   
 *  require_once('MkEncrypt.php');
 * 
 * 3、设置页面访问密码 
 *  MkEncrypt('页面密码');
 * 
********************************************/
 
// 密码 Cookie 加密钥
if(!defined('MK_ENCRYPT_SALT'))
    define('MK_ENCRYPT_SALT', 'Kgs$JC!V');
 
/**
 * 设置访问密码
 * 
 * @param $password  访问密码
 * @param $pageid    页面唯一 ID 值,用于区分同一网站的不同加密页面
 */
function MkEncrypt($password, $pageid = 'default') {
    $pageid     = md5($pageid);
    $md5pw      = md5(md5($password).MK_ENCRYPT_SALT);
    $postpwd    = isset($_POST['pagepwd']) ? addslashes(trim($_POST['pagepwd'])) : '';
    $cookiepwd  = isset($_COOKIE['mk_encrypt_'.$pageid]) ? addslashes(trim($_COOKIE['mk_encrypt_'.$pageid])) : '';
    
    if($cookiepwd == $md5pw) return;    // Cookie密码验证正确
    
    if($postpwd == $password) {         // 提交的密码正确
        setcookie('mk_encrypt_' . $pageid, $md5pw, time() + 3600000, '/');
        return;
    }
?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <meta charset="UTF-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="renderer" content="webkit"> 
    <meta name="author" content="mengkun">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>该页面已被加密</title>
    <style type="text/css">
    *{font-family:"Microsoft Yahei",微软雅黑,"Helvetica Neue",Helvetica,"Hiragino Sans GB","WenQuanYi Micro Hei",sans-serif;box-sizing:border-box;margin:0px;padding:0px;font-size:14px;-webkit-transition:.2s;-moz-transition:.2s;-ms-transition:.2s;-o-transition:.2s;transition:.2s}
    html,body{width:100%;height:100%}
    body{background-color:#F4F6F9;color:#768093}
    input,button{font-size:1em;border-radius:3px;-webkit-appearance:none}
    input{width:100%;padding:5px;box-sizing:border-box;border:1px solid #e5e9ef;background-color:#f4f5f7;resize:vertical}
    input:focus{background-color:#fff;outline:none}
    button{border:0;background:#6abd09;color:#fff;cursor:pointer;opacity:1;user-select:none}
    button:hover,button:focus{opacity:.9}
    button:active{opacity:1}
    .main{width:100%;max-width:500px;height:300px;padding:30px;background-color:#fff;border-radius:2px;box-shadow:0 10px 60px 0 rgba(29,29,31,0.09);transition:all .12s ease-out;position:absolute;left:0;top:0;bottom:0;right:0;margin:auto;text-align:center}
    .alert{width:80px}
    .mk-side-form{margin-bottom:28px}
    .mk-side-form input{float:left;padding:2px 10px;width:77%;height:37px;border:1px solid #ebebeb;border-right-color:transparent;border-radius:2px 0 0 2px;line-height:37px}
    .mk-side-form button{position:relative;overflow:visible;width:23%;height:37px;border-radius:0 2px 2px 0;text-transform:uppercase}
    .pw-tip{font-weight:normal;font-size:26px;text-align:center;margin:25px auto}
    #pw-error {color: red;margin-top: 15px;margin-bottom: -20px;}
    .return-home{text-decoration:none;color:#b1b1b1;font-size:16px}
    .return-home:hover{color:#1E9FFF;letter-spacing:5px}
    </style>
</head>
<body>
    <div class="main">
        <svg class="alert" viewBox="0 0 1084 1024" xmlns="http://www.w3.org/2000/svg" width="80" height="80">
            <defs><style/></defs>
            <path d="M1060.744 895.036L590.547 80.656a55.959 55.959 0 0 0-96.919 0L22.588 896.662a55.959 55.959 0 0 0 48.43 83.907h942.14a55.959 55.959 0 0 0 47.525-85.534zm-470.619-85.172a48.008 48.008 0 1 1-96.015 0v-1.567a48.008 48.008 0 1 1 96.015 0v1.567zm0-175.345a48.008 48.008 0 1 1-96.015 0V379.362a48.008 48.008 0 1 1 96.015 0v255.157z" fill="#FF9800"/>
        </svg>
        
        <form action="" method="post" class="mk-side-form">
            <h2 class="pw-tip">该页面已被加密</h2>
            <input type="password" name="pagepwd" placeholder="请输入访问密码查看" required><button type="submit">提交</button>
            <?php if($postpwd): ?>
            <p id="pw-error">Oops!密码不对哦~</p>
            <script>setTimeout(function() {document.getElementById("pw-error").style.display = "none"}, 2000);</script>
            <?php endif; ?>
        </form>
        <a href="/" class="return-home" title="点击回到网站首页">- 返回首页 - </a>
    </div>
</body>
</html>
<?php
    exit();
}

 

使用演示

<?php

require_once('MkEncrypt.php');  //调用 MkEncrypt.php 文件
MkEncrypt('1230');   // 设置密码为 1230

$pass_key = 'a123456zhangfei';
$pass_time = date("YmdHi",time());
echo "本次动态口令:".substr(base_convert(md5($pass_key.$pass_time),16,10),5,6)."<br />";
echo "本次口令获取时间:".date('Y-m-d H:i',time())."<br />";
echo "口令下次更新时间:".date('Y-m-d H:i',strtotime("+1 minute"))."<br />";
?>

 

 

 

样式2

来源:https://www.moerats.com/archives/555/

Github地址:https://github.com/Pearlulu/h5ai_dplayer_hls

在需要加密的页面 添加include 'login.php';代码,修改用户名密码在login.php页面文件

 

index.php 文件

<?php
include 'login.php';

define('H5AI_VERSION', '0.29.2');
define('MIN_PHP_VERSION', '5.5.0');

if (!function_exists('version_compare') || version_compare(PHP_VERSION, MIN_PHP_VERSION, '<')) {
    header('Content-type: text/plain;charset=utf-8');
    exit('[ERR] h5ai requires PHP ' . MIN_PHP_VERSION . ' or later, but found PHP ' . PHP_VERSION);
}

if (substr(H5AI_VERSION, 0, 1) === '{') {
    header('Content-type: text/plain;charset=utf-8');
    exit('[ERR] h5ai sources must be preprocessed to work correctly');
}

require_once __DIR__ . '/../private/php/class-bootstrap.php';
Bootstrap::run();

 

login.php 文件

<?php 

$cat_user = 'admin';
$cat_password = 'admin';

$cat_salt = 'salt';

//如果cookie空,POST用户名密码不为空
if(isset($_POST['username']) && isset($_POST['password'])) {
    //验证输入密码是否匹配
    if(md5($_POST['username'].$_POST['password'].$cat_salt)===md5($cat_user.$cat_password.$cat_salt)) {
        //设置cookie
        setcookie('verify',md5($cat_user.$cat_password.$cat_salt),time()+86400*30);
        header("location:/");
    } else {
        die('帐号或者密码错误,请返回重试!');
    }
}

//检测cookie
if(empty($_COOKIE['verify']) || $_COOKIE['verify']!=md5($cat_user.$cat_password.$cat_salt)) {
    ?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <style type="text/css">
    * {
        box-sizing: border-box;
    }
    input {
        outline: 0!important;
    }
    button, input, optgroup, select, textarea {
        font-family: sans-serif;
        font-size: 16px;
        line-height: 1.15;
        margin: 0;
    }
    input[type=password], input[type=text], select, textarea {
        padding: .5em 0;
        line-height: 1;
        display: block;
        border: 0;
        border-bottom: 1px solid #ddd;
        transition: border .2s ease;
        width: 100%;
        background: transparent;
    }
    input[type=submit] {
        border: 0;
        padding: .5em 1em;
        border-radius: .1em;
        cursor: pointer;
        background: #2196f3;
        color: #fff;
        border: 1px solid rgba(0,0,0,.05);
        box-shadow: 0 0 5px rgba(0,0,0,.05);
        transition: all .1s ease;
    }
    input[type=submit]:hover {
        background-color: #1e88e5;
    }
    #login {
        background: #fff;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }
    #login form {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        max-width: 16em;
        width: 90%;
    }
    #login h1 {
        text-align: center;
        font-size: 2.5em;
        margin: -0.4em 0 .67em;
    }
    #login input {
        width: 100%;
        margin: .5em 0 0;
    }
    #login input[type=password], #login input[type=text] {
        padding: .5em 1em;
        border: 1px solid #e9e9e9;
        transition: border .2s ease;
        color: #333;
    }
    #login input[type=password]:focus, #login input[type=password]:hover, #login input[type=text]:focus, #login input[type=text]:hover {
        border-color: #9f9f9f;
    }
    </style>
</head>
<body>
    <div id="login">
        <form action="/" method="post">
            <h1>请登录</h1>
            <input name="username" type="text" placeholder="用户名"> 
            <input name="password" type="password" placeholder="密码">
            <input type="submit" value="登录">
        </form>
    </div>
</body>
</html>
    <?php
    die(1);
}

?>

 

赞() 打赏
未经允许不得转载:我的博客 » 为任意PHP页面设置访问密码
分享到: 更多 (0)

评论 4

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    这是评论测试

    DUX主题小秘7个月前 (09-17)回复
    • 飞歌导航东方红烦得很

      themebetter主题小秘3周前 (04-08)回复
  2. #2

    这是新的一条评论

    DUX主题小秘7个月前 (09-17)回复
  3. #3

    未来一年,中国的移动支付市场会是什么样?

    DUX主题小秘7个月前 (09-17)回复

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

大前端WP主题 更专业 更方便

联系我们 联系我们