给网站加首页简单加一个密码访问,PHP为任意页面

密码正确才能进去页面,进入后会存下cookies值,下一次登录的时候则不需要再次输入了;
1.把下面的代码放进你要加密的PHP页面里面,比如你要给首页:index.php 加密,就把下面代码放在index.php 的最前面
<?php
require_once('MkEncrypt.php');
MkEncrypt('1234');     //这个就是密码
?>
2.新建PHP文件,命名为:MkEncrypt.php,把下面代码放进这个新建为PHP文件里面
  1. <?php
  2. /********************************************
  3. * 使用方法:
  4. *
  5. * 1、将本段代码保存为 MkEncrypt.php
  6. *
  7. * 2、在要加密的页面前面引入这个 php 文件
  8. * require_once('MkEncrypt.php');
  9. * 3、设置页面访问密码
  10. * MkEncrypt('页面密码');
  11. *
  12. ********************************************/
  13. // 密码 Cookie 加密盐
  14. if(!defined('MK_ENCRYPT_SALT'))
  15. define('MK_ENCRYPT_SALT', 'Kgs$JC!V');

  16. /**
  17. * 设置访问密码
  18. *
  19. * @param $password 访问密码
  20. * @param $pageid 页面唯一 ID 值,用于区分同一网站的不同加密页面
  21. */
  22. function MkEncrypt($password, $pageid = 'default') {
  23. $pageid = md5($pageid);
  24. $md5pw = md5(md5($password).MK_ENCRYPT_SALT);
  25. $postpwd = isset($_POST['pagepwd']) ? addslashes(trim($_POST['pagepwd'])) : '';
  26. $cookiepwd = isset($_COOKIE['mk_encrypt_'.$pageid]) ? addslashes(trim($_COOKIE['mk_encrypt_'.$pageid])) : '';

  27. if($cookiepwd == $md5pw) return; // Cookie密码验证正确

  28. if($postpwd == $password) { // 提交的密码正确
  29. setcookie('mk_encrypt_' . $pageid, $md5pw, time() + 3600000, '/');
  30. return;
  31. }
  32. ?>
  33. <html>
  34. <head>
  35. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  36. <meta charset="UTF-8">
  37. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  38. <meta name="renderer" content="webkit">
  39. <meta name="author" content="mengkun">
  40. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  41. <title>该页面已被加密</title>
  42. <style type="text/css">
  43. *{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}
  44. html,body{width:100%;height:100%}
  45. body{background-color:#F4F6F9;color:#768093}
  46. input,button{font-size:1em;border-radius:3px;-webkit-appearance:none}
  47. input{width:100%;padding:5px;box-sizing:border-box;border:1px solid #e5e9ef;background-color:#f4f5f7;resize:vertical}
  48. input:focus{background-color:#fff;outline:none}
  49. button{border:0;background:#6abd09;color:#fff;cursor:pointer;opacity:1;user-select:none}
  50. button:hover,button:focus{opacity:.9}
  51. button:active{opacity:1}
  52. .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}
  53. .alert{width:80px}
  54. .mk-side-form{margin-bottom:28px}
  55. .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}
  56. .mk-side-form button{position:relative;overflow:visible;width:23%;height:37px;border-radius:0 2px 2px 0;text-transform:uppercase}
  57. .pw-tip{font-weight:normal;font-size:26px;text-align:center;margin:25px auto}
  58. #pw-error {color: red;margin-top: 15px;margin-bottom: -20px;}
  59. .return-home{text-decoration:none;color:#b1b1b1;font-size:16px}
  60. .return-home:hover{color:#1E9FFF;letter-spacing:5px}
  61. </style>
  62. </head>
  63. <body>
  64. <div class="main">
  65. <svg class="alert" viewBox="0 0 1084 1024" xmlns="http://www.w3.org/2000/svg" width="80" height="80">
  66. <defs><style/></defs>
  67. <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"/>
  68. </svg>

  69. <form action="" method="post" class="mk-side-form">
  70. <h2 class="pw-tip">该页面已被加密</h2>
  71. <input type="password" name="pagepwd" placeholder="请输入访问密码查看" required><button type="submit">提交</button>
  72. <?php if($postpwd): ?>
  73. <p id="pw-error">Oops!密码不对哦~</p>
  74. <script>setTimeout(function() {document.getElementById("pw-error").style.display = "none"}, 2000);</script>
  75. <?php endif; ?>
  76. </form>
  77. <a href="/" class="return-home" title="点击回到网站首页">- 返回首页 - </a>
  78. </div>
  79. </body>
  80. </html>
  81. <?php
  82. exit();
  83. }
复制代码






上一篇:Windows7-8上安装python后报api-ms-win-core-path-l1-1-0.dll错误的解决方法
下一篇:使用 Sticky-Kit实现基于jQuery的元素侧边跟随滚动固定效果
如无回复请发邮件