当前位置: 代码网 > it编程>编程语言>Php > PHP如何实现给页面设置独立访问密码

PHP如何实现给页面设置独立访问密码

2024年05月15日 Php 我要评论
php网页如果需要查看信息必须输入密码,验证后才可显示出内容的代码如何实现?对某些php页面设置单独的访问密码,如果密码不正确则无法查看内容,相当于对页面进行了一个加密。效果截图使用方法和步骤新建一个

php网页如果需要查看信息必须输入密码,验证后才可显示出内容的代码如何实现?

对某些php页面设置单独的访问密码,如果密码不正确则无法查看内容,相当于对页面进行了一个加密。

效果截图

使用方法和步骤

新建一个mkencrypt.php文件在根目录下或者同级目录下。

mkencrypt.php里面添加以下代码:

<?php
if(!defined('mk_encrypt_salt'))
define('mk_encrypt_salt', 'kgs$jc!v');
/**
* 设置访问密码  
* @param $password 访问密码
* @param $pageid 页面唯一 id 值,用于区分同一网站的不同加密页面
*/
function mkencrypt($password, $pageid = 'default') {
$pageid = md5($pageid);
$md5pw = md5(md5($password).mk_encrypt_salt);
$postpwd = isset($_post['pagepwd']) ? addslashes(trim($_post['pagepwd'])) : '';
$cookiepwd = isset($_cookie['mk_encrypt_'.$pageid]) ? addslashes(trim($_cookie['mk_encrypt_'.$pageid])) : '';
if($cookiepwd == $md5pw) return; // cookie密码验证正确
if($postpwd == $password) { // 提交的密码正确
setcookie('mk_encrypt_' . $pageid, $md5pw, time() + 3600000, '/');
return;
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="renderer" content="webkit">
<meta name="author" content="mengkun">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>该页面已被加密</title>
<style type="text/css">
*{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}
</style>
</head>
<body>
<div class="main">
<svg class="alert" viewbox="0 0 1084 1024" xmlns="http://www.w3.org/2000/svg" width="80" height="80">
<defs><style/></defs>
<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"/>
</svg>
<form action="" method="post" class="mk-side-form">
<h2 class="pw-tip">该页面已被加密</h2>
<input type="password" name="pagepwd" placeholder="请输入访问密码查看" required><button type="submit">访问</button>
<?php if($postpwd): ?>
<p id="pw-error">密码不对哦~</p>
<script>settimeout(function() {document.getelementbyid("pw-error").style.display = "none"}, 2000);</script>
<?php endif; ?>
</form>
<a href="/" rel="external nofollow"  class="return-home" title="点击回到网站首页">- 返回首页 - </a>
</div>
</body>
</html>
<?php
exit();
}

把下面的代码放在你需要加密的页进行调用

<?php
    require_once('mkencrypt.php');
    mkencrypt('123456'); 
    ?>

mkencrypt(‘123456’);括号里面123456修改成你需要设置的密码。

密码正确才能进去页面,进入后会存下cookies值,下一次登录的时候则不需要再次输入了,只要是php程序都是支持这段代码的。

方法补充

除了上文的方法,小编还为大家整理了一些php为页面加密的方法,希望对大家有所帮助

对某些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 = &#39;http://&#39;.$_server[&#39;server_name&#39;].&#39;:&#39;.$_server["server_port"].$_server["request_uri"];
//echo $url;
if (!session_id()){session_start();};
if(isset($_get[&#39;close&#39;])){  
$url = $_get[&#39;url&#39;]; 
unset($_session[&#39;recheck&#39;]);
}
if(isset($_post[&#39;password&#39;]) && $_post[&#39;password&#39;] == &#39;123456&#39;){
    $_session[&#39;recheck&#39;] = 1;
    header(&#39;location:&#39;.$url);
}
if(!isset($_session[&#39;recheck&#39;])){
    exit(&#39;<div id="divcss">
        <form method="post">
            请输入独立访问密码:<input type="password" name="password" />
            <input type="submit" value="确定" />(密码:123456)
        </form>
    </div>
    &#39;);
}
?>
<div id="footer"><a href="?close=yes&url=<?php echo $url?>" rel="external nofollow" ><font color="#ffffff">安全退出本页面</font></a></div>
</body>
</html>

在需要进行设置独立密码访问的页面包含该php文件即可,这样就能保证只有输入正确的访问密码后才可以访问指定页面了;也可以稍作修改封装成函数直接插入到需要设置访问密码的页面顶部,这样就可以每个页面设置不一样的访问密码了!

<?php include(‘recheck.php'); ?>

到此这篇关于php如何实现给页面设置独立访问密码的文章就介绍到这了,更多相关php页面设置访问密码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com