对某些php页面设置单独的访问密码,如果密码不正确则无法查看内容,相当于对页面进行了一个加密。只需要将以下php文件包含在你需要设置独立访问密码的最前面就可以了。
recheck.php<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>title</title><style>#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}</style></head><body><?php//所有需要输出二次密码打开的页面,只需要将本php文件进行包含即可$url = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];//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"><form method="post">请输入独立访问密码:<input type="password" name="password" /><input type="submit" value="确定" />(密码:123456)</form></div>');}?><div id="footer"><a href="?close=yes&url=<?php echo $url?>"><font color="#FFFFFF">安全退出本页面</font></a></div></body></html>
?
在需要进行设置独立密码访问的页面包含该php文件即可,这样就能保证只有输入正确的访问密码后才可以访问指定页面了;也可以稍作修改封装成函数直接插入到需要设置访问密码的页面顶部,这样就可以每个页面设置不一样的访问密码了!
<?php include(‘recheck.php’); ?>
1 条评论
该源码存在bug,如果有多个页面使用本方法,会导致一旦知道其中一个密码,即可访问其他页面。避免此bug请将session的值设置为加密“密码”,而不是一个固定的“recheck”!