我就是我,一个平凡的社会人

一个PHP页面,访问的时候显示需要输入密码才能查看,输入密码正确后写出一个cookie,有效期1小时。

当网页再次刷新或者重新打开该网页的时候读取cookie,cookie有效的话,就不验证密码直接显示加密后的内容。

<?php

$pass='123456';//你设置的正确密码
$cook_pass=isset($_COOKIE['pw'])>$_COOKIE['pw']:'';
if ($cook_pass!=$pass){
    $form_pass=isset($_POST['pw'])>$_POST['pw']:'';
    if ($form_pass==$pass){//密码正确,保存COOKIE
        set_cookie('pw',$form_pass);
    }else{//密码错误或者没有密码,显示登录内容
        echo '<form method=post action=$PHP_SELF>

密码:<input type=passwd name=pw>
<input type=submit value='登录'>
</form>';

        exit;
    }
}

?>
上面的代码在苹果cms里会报错,下面才是正确的代码

/**

  • 检验私密页面的密码
    *
  • 使用方法,在有需要的加密的页面最开始补充下面这行代码
    *
  • <?php
  • include('password.php');
  • ?>
    *
  • 然后把本页代码命名为password.php即可.
  • PS:需要退出登录就直接在页面的后面加入请求password.php?action=logout
    */

$page_pwd = md5('小小酥'); //你要设置的密码
$page_cookname = 'my-page-password'; //你要设置的cookie名
$page_now = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
$action = @$_GET['action'];
$page_postpwd = @$_POST['page_pwd'];
$page_cookiepwd = @$_COOKIE["$page_cookname"];
$page_cookietime = time() + 60 60 24 * 7;

//输出网页的头部
$head = '

<head>
<meta charset="utf-8">
<title>product</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<style type="text/css">body,button,input,select,textarea,h1,h2,h3,h4,h5,h6 {
    font-family: Microsoft YaHei, "宋体", Tahoma, Helvetica, Arial, "\5b8b\4f53", sans-serif;
}
</style>

';

//退出登录
if ($action == "logout") {

setcookie($page_cookname, "", time() - 1);
echo '
<meta http-equiv="refresh" content="2";URL='.$page_now.'>

</head>
<body>
<div class="container-fluid">

<p>退出成功,2秒后自动跳转</p>
<a role="button" class="btn btn-success" href='.$page_now.'>点此马上跳转</a>

</div>
</body>
</html>
';
exit;

}

//有输入密码
if ($page_postpwd != "") {

//密码错误
if (md5("$page_postpwd") != $page_pwd) {
    echo $head;
    echo '
        <meta http-equiv="refresh" content="2";URL='.$page_now.'>

        </head>
        <body>
        <div class="container-fluid">

        <p>密码错误,2秒后自动跳转</p>
        <a role="button" class="btn btn-success" href='.$page_now.'>点此马上跳转</a>

        </div>
        </body>
        </html>
        ';
    exit;
}
//密码正确
else {
    //设置Cookies
    setcookie($page_cookname, md5("$page_postpwd"), $page_cookietime);
    echo $head;
    echo '
        <meta http-equiv="refresh" content="2";URL='.$page_now.'>
        </head>
        <body>
        <div class="container-fluid">

        <p>密码正确,2秒后自动跳转</p>
        <a role="button" class="btn btn-success" href='.$page_now.'>点此马上跳转</a>

        </div>
        </body>
        </html>
        ';
    exit;
}

}
//没输入密码
if ($page_cookiepwd != $page_pwd) {

echo $head;
echo '
    </head>
    <body>
    <div class="row text-center vertical-middle-sm">
    <div class="col-sm-12">
    <div class="container-fluid">
    <br>
    <p>这是一个私人页面,请输入您的密码</p>
    <br>
    <form method="POST">
        <div class="form-group">
    <input type="text" class="form-control" name="page_pwd" placeholder="请输入您的密码">
    <br>
    <button type="submit" class="btn btn-default">确认</button>

    </div>
    </div>
        </div>
    </form>
    </div>
    </body>
    </html>
    ';
exit;

}
?>
加在苹果cms的index.php文件里,注意不要有php的开头和结尾

    有你的评论,我才会进步