PHP如何實(shí)現(xiàn)給頁面設(shè)置獨(dú)立訪問密碼
PHP網(wǎng)頁如果需要查看信息必須輸入密碼,驗(yàn)證后才可顯示出內(nèi)容的代碼如何實(shí)現(xiàn)?
對(duì)某些php頁面設(shè)置單獨(dú)的訪問密碼,如果密碼不正確則無法查看內(nèi)容,相當(dāng)于對(duì)頁面進(jìn)行了一個(gè)加密。
效果截圖
使用方法和步驟
新建一個(gè)MkEncrypt.php文件在根目錄下或者同級(jí)目錄下。
MkEncrypt.php里面添加以下代碼:
<?php if(!defined('MK_ENCRYPT_SALT')) define('MK_ENCRYPT_SALT', 'Kgs$JC!V'); /** * 設(shè)置訪問密碼 * @param $password 訪問密碼 * @param $pageid 頁面唯一 ID 值,用于區(qū)分同一網(wǎng)站的不同加密頁面 */ 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密碼驗(yàn)證正確 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="請(qǐng)輸入訪問密碼查看" required><button type="submit">訪問</button> <?php if($postpwd): ?> <p id="pw-error">密碼不對(duì)哦~</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="點(diǎn)擊回到網(wǎng)站首頁">- 返回首頁 - </a> </div> </body> </html> <?php exit(); }
把下面的代碼放在你需要加密的頁進(jìn)行調(diào)用
<?php require_once('MkEncrypt.php'); MkEncrypt('123456'); ?>
MkEncrypt(‘123456’);括號(hào)里面123456修改成你需要設(shè)置的密碼。
密碼正確才能進(jìn)去頁面,進(jìn)入后會(huì)存下cookies值,下一次登錄的時(shí)候則不需要再次輸入了,只要是PHP程序都是支持這段代碼的。
方法補(bǔ)充
除了上文的方法,小編還為大家整理了一些PHP為頁面加密的方法,希望對(duì)大家有所幫助
對(duì)某些php頁面設(shè)置單獨(dú)的訪問密碼,如果密碼不正確則無法查看內(nèi)容,相當(dāng)于對(duì)頁面進(jìn)行了一個(gè)加密。只需要將以下php文件包含在你需要設(shè)置獨(dú)立訪問密碼的最前面就可以了。
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文件進(jìn)行包含即可 $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"> 請(qǐng)輸入獨(dú)立訪問密碼:<input type="password" name="password" /> <input type="submit" value="確定" />(密碼:123456) </form> </div> '); } ?> <div id="footer"><a href="?close=yes&url=<?php echo $url?>" rel="external nofollow" ><font color="#FFFFFF">安全退出本頁面</font></a></div> </body> </html>
在需要進(jìn)行設(shè)置獨(dú)立密碼訪問的頁面包含該php文件即可,這樣就能保證只有輸入正確的訪問密碼后才可以訪問指定頁面了;也可以稍作修改封裝成函數(shù)直接插入到需要設(shè)置訪問密碼的頁面頂部,這樣就可以每個(gè)頁面設(shè)置不一樣的訪問密碼了!
<?php include(‘recheck.php'); ?>
到此這篇關(guān)于PHP如何實(shí)現(xiàn)給頁面設(shè)置獨(dú)立訪問密碼的文章就介紹到這了,更多相關(guān)PHP頁面設(shè)置訪問密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php 靜態(tài)屬性和靜態(tài)方法區(qū)別詳解
這篇文章主要介紹了php 靜態(tài)屬性和靜態(tài)方法區(qū)別詳解,需要的朋友可以參考下2017-04-04詳解Swoole跟傳統(tǒng)的web開發(fā)的區(qū)別
Swoole高效跟傳統(tǒng)的web開發(fā)有什么區(qū)別,除了傳統(tǒng)的LAMP/LNMP同步開發(fā)模式,swoole的異步開發(fā)模式是怎么樣的。本文帶著大家來詳細(xì)介紹一下。2021-05-05PHP+ajax實(shí)現(xiàn)獲取新聞數(shù)據(jù)簡(jiǎn)單示例
這篇文章主要介紹了PHP+ajax實(shí)現(xiàn)獲取新聞數(shù)據(jù),涉及php ajax交互獲取信息及json格式處理的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05PHP實(shí)現(xiàn)分布式memcache設(shè)置web集群session同步的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)分布式memcache設(shè)置web集群session同步的方法,結(jié)合實(shí)例形式分析了php設(shè)置與使用memcache實(shí)現(xiàn)web集群session同步的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-04-04PHP連接SQLServer2005的實(shí)現(xiàn)方法(附ntwdblib.dll下載)
為了php連接sql2005 ,我在網(wǎng)絡(luò)上找了一大堆資料在我的csdn博客中.晚上3:05分時(shí)候終于搞定了2012-07-07