PHP网页如果需要查看信息必须输入密码,验证后才可显示出内容的代码如何实现?
对某些php页面设置单独的访问密码,如果密码不正确则无法查看内容,相当于对页面进行了一个加密。
效果截图
使用方法和步骤
新建一个MkEncrypt.php文件在根目录下或者同级目录下。
MkEncrypt.php里面添加以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <title>该页面已被加密</title> *{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} <div class = "main" > <h2 class = "pw-tip" >该页面已被加密</h2> <button type= "submit" >访问</button> <p id= "pw-error" >密码不对哦~</p> setTimeout( function () {document.getElementById( "pw-error" ).style.display = "none" }, 2000); <a href= "/" rel= "external nofollow" class = "return-home" title= "点击回到网站首页" >- 返回首页 - </a> </div> <?php exit (); } |
把下面的代码放在你需要加密的页进行调用
1 |
MkEncrypt(‘123456’);括号里面123456修改成你需要设置的密码。
密码正确才能进去页面,进入后会存下cookies值,下一次登录的时候则不需要再次输入了,只要是PHP程序都是支持这段代码的。
方法补充
除了上文的方法,小编还为大家整理了一些PHP为页面加密的方法,希望对大家有所帮助
对某些php页面设置单独的访问密码,如果密码不正确则无法查看内容,相当于对页面进行了一个加密。只需要将以下php文件包含在你需要设置独立访问密码的最前面就可以了。
recheck.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <title>title</title> #divcss{margin:300 auto;width:400px;height:40px;} #footer { height: 40px; line-height: 40px; position: fixed; bottom: 0; width: 100%; text-align: center; background: #373d41; color: #ffffff; font-family: Arial; font-size: 16px; letter-spacing: 1px; } a {text-decoration: none} <?php //所有需要输出二次密码打开的页面,只需要将本php文件进行包含即可 //echo $url; if (!session_id()){session_start();}; if (isset( $_GET [ 'close' ])){ $url = $_GET [ 'url' ]; unset( $_SESSION [ 'recheck' ]); } if (isset( $_POST [ 'password' ]) && $_POST [ 'password' ] == '123456' ){ $_SESSION [ 'recheck' ] = 1; header( 'location:' . $url ); } if (!isset( $_SESSION [ 'recheck' ])){ exit ('<div id= "divcss" ></div> |
请输入独立访问密码:(密码:123456)
‘);
}
?>