PHP为任意页面设访问密码,需要登陆才能访问

PHP为任意页面设访问密码,需要登陆才能访问


使用方法

把下面的代码存为php文件,下面的整段代码是验证过程,然后在你入口页进行调用

例如命名为MkEncrypt.php,那么在入口页

进行

require_once(‘MkEncrypt.php’);

然后设置密码为

MkEncrypt(‘1234’);

此时密码为1234

则密码正确才能进去页面,进入后会存下cookies值,下一次登录的时候则不需要再次输入了;如果不想给人看了,只需要更改密码,cookies就会立即失效,必须要重新登陆才能看。

代码简介

为你的页面 支持 加密访问 无论什么程序 只要是PHP程序 都是支持这代码的 来实现加密访问你的加密页面 或文章等等的页面 不保证其他程序可以正常使用 麻烦先测试!

代码如下

<?php
 
/********************************************
 * 使用方法:
 * 
 * 1、将本段代码保存为 MkEncrypt.php
 * 
 * 2、在要加密的页面前面引入这个 php 文件   
 *  require_once('MkEncrypt.php');
 * 
 * 3、设置页面访问密码 
 *  MkEncrypt('页面密码');
 * 
********************************************/
 
// 密码 Cookie 加密盐
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">Oops!密码不对哦~</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();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的AJAX MySQL PHP登录功能的实现过程: 1. 创建数据库表 首先,我们需要在MySQL数据库中创建一个名为users的表,该表包含以下字段: - id (自增主键) - username (用户名) - password (密码) 2. 创建登录页面 创建一个HTML登录页面,其中包含一个用户名和密码输入框以及一个“登录”按钮。当用户单击登录按钮时,使用AJAX将用户名和密码发送到服务器。 3. 编写AJAX代码 使用jQuery编写AJAX代码,将用户名和密码发送到服务器,并处理服务器返回的响应。如果登录成功,则重定向到其他页面;否则,显示错误消息。 ```javascript $("#login-btn").click(function() { var username = $("#username").val(); var password = $("#password").val(); $.ajax({ url: "login.php", type: "POST", data: { username: username, password: password }, success: function(response) { if(response == "success") { window.location.href = "dashboard.php"; } else { $("#error-message").html("用户名或密码错误"); } } }); }); ``` 4. 编写PHP代码 在服务器端,我们需要编写一个PHP脚本来处理登录请求。该脚本首先从POST请求中获取用户名和密码,并检查它们是否与数据库中的用户记录匹配。如果匹配,则返回“success”响应,否则返回“error”响应。 ```php <?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); // 获取POST请求中的用户名和密码 $username = $_POST["username"]; $password = $_POST["password"]; // 查询数据库中是否存在该用户 $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $query); if(mysqli_num_rows($result) == 1) { // 登录成功 echo "success"; } else { // 登录失败 echo "error"; } ?> ``` 5. 测试登录功能 现在您可以在浏览器中访问登录页面,并使用任意用户名和密码进行登录测试。如果一切正常,则应该在成功登录后重定向到其他页面。如果登录失败,则应该显示错误消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

主题模板站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值