本篇内容就如标题所说的,用于给任意的页面设置访问密码。全部代码都在下头了↓↓↓使用后的效果同https://mkblog.cn/1634/

本篇完……(溜了溜了,继续潜水

PHP

  1. <?php
  2.  
  3. /********************************************
  4. * 使用方法:
  5. *
  6. * 1、将本段代码保存为 MkEncrypt.php
  7. *
  8. * 2、在要加密的页面前面引入这个 php 文件
  9. * require_once('MkEncrypt.php');
  10. *
  11. * 3、设置页面访问密码
  12. * MkEncrypt('页面密码');
  13. *
  14. ********************************************/
  15.  
  16. // 密码 Cookie 加密盐
  17. if(!defined('MK_ENCRYPT_SALT'))
  18. define('MK_ENCRYPT_SALT', 'Kgs$JC!V');
  19.  
  20. /**
  21. * 设置访问密码
  22. *
  23. * @param $password 访问密码
  24. * @param $pageid 页面唯一 ID 值,用于区分同一网站的不同加密页面
  25. */
  26. function MkEncrypt($password, $pageid = 'default') {
  27. $pageid = md5($pageid);
  28. $md5pw = md5(md5($password).MK_ENCRYPT_SALT);
  29. $postpwd = isset($_POST['pagepwd']) ? addslashes(trim($_POST['pagepwd'])) : '';
  30. $cookiepwd = isset($_COOKIE['mk_encrypt_'.$pageid]) ? addslashes(trim($_COOKIE['mk_encrypt_'.$pageid])) : '';
  31. if($cookiepwd == $md5pw) return; // Cookie密码验证正确
  32. if($postpwd == $password) { // 提交的密码正确
  33. setcookie('mk_encrypt_' . $pageid, $md5pw, time() + 3600000, '/');
  34. return;
  35. }
  36. ?>
  37. <html>
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  40. <meta charset="UTF-8">
  41. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  42. <meta name="renderer" content="webkit">
  43. <meta name="author" content="mengkun">
  44. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  45. <title>该页面已被加密</title>
  46. <style type="text/css">
  47. *{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}
  48. html,body{width:100%;height:100%}
  49. body{background-color:#F4F6F9;color:#768093}
  50. input,button{font-size:1em;border-radius:3px;-webkit-appearance:none}
  51. input{width:100%;padding:5px;box-sizing:border-box;border:1px solid #e5e9ef;background-color:#f4f5f7;resize:vertical}
  52. input:focus{background-color:#fff;outline:none}
  53. button{border:0;background:#6abd09;color:#fff;cursor:pointer;opacity:1;user-select:none}
  54. button:hover,button:focus{opacity:.9}
  55. button:active{opacity:1}
  56. .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}
  57. .alert{width:80px}
  58. .mk-side-form{margin-bottom:28px}
  59. .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}
  60. .mk-side-form button{position:relative;overflow:visible;width:23%;height:37px;border-radius:0 2px 2px 0;text-transform:uppercase}
  61. .pw-tip{font-weight:normal;font-size:26px;text-align:center;margin:25px auto}
  62. #pw-error {color: red;margin-top: 15px;margin-bottom: -20px;}
  63. .return-home{text-decoration:none;color:#b1b1b1;font-size:16px}
  64. .return-home:hover{color:#1E9FFF;letter-spacing:5px}
  65. </style>
  66. </head>
  67. <body>
  68. <div class="main">
  69. <svg class="alert" viewBox="0 0 1084 1024" xmlns="http://www.w3.org/2000/svg" width="80" height="80">
  70. <defs><style/></defs>
  71. <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"/>
  72. </svg>
  73. <form action="" method="post" class="mk-side-form">
  74. <h2 class="pw-tip">该页面已被加密</h2>
  75. <input type="password" name="pagepwd" placeholder="请输入访问密码查看" required><button type="submit">提交</button>
  76. <?php if($postpwd): ?>
  77. <p id="pw-error">Oops!密码不对哦~</p>
  78. <script>setTimeout(function() {document.getElementById("pw-error").style.display = "none"}, 2000);</script>
  79. <?php endif; ?>
  80. </form>
  81. <a href="/" class="return-home" title="点击回到网站首页">- 返回首页 - </a>
  82. </div>
  83. </body>
  84. </html>
  85. <?php
  86. exit();
  87. }
 复制 文本

后记

是不是觉得有点水?我也这么觉得,哈哈哈哈……

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。