对于某些需要密码访问的页面,放置下面例子代码到相应页面即可。
注意:代码放置位置,到页面最上方空出一行,然后粘贴,最后改密码,访问之。
例一、
- <?php
- $password = "123456"; // 这里是密码
- $p = "";
- if(isset($_COOKIE["isview"]) and $_COOKIE["isview"] == $password){
- $isview = true;
- }else{
- if(isset($_POST["pwd"])){
- if($_POST["pwd"] == $password){
- setcookie("isview",$_POST["pwd"],time()+3600*3);
- $isview = true;
- }else{
- $p = (emptyempty($_POST["pwd"])) ? "需要输入密码才能查看!" : "密码错误!请重新输入!";
- }
- }else{
- $isview = false;
- $p = "需要输入密码才能查看!";
- }
- }
- if(!$isview){ ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>请输入密码</title>
- </head>
- <body>
- <form action="" method="post">
- 输入密码:<input type="password" name="pwd" /> <input type="submit" value="查看" />
- <p><?php echo $p; ?></p>
- </form>
- </body>
- </html>
- <?php }else{ ?>
- 这里是输入密码后查看的页面代码 OK!
- <?php } ?>
例二、
- <?php
- session_start();
- if(isset($_POST['password']) && $_POST['password'] == '123456'){
- $_SESSION['ok'] = 1;
- header('location:?');
- }
- if(!isset($_SESSION['ok'])){
- exit('
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>请输入密码</title>
- </head>
- <body>
- <form method="post">
- 密码:<input type="password" name="password" /><input type="submit" value="登陆" />
- </form>
- </body>
- </html>
- ');
- }
- ?>
注意:其中 123456 为密码,修改之。
完毕!