How to implement PHP to set an independent access password to the page
PHP web page if you need to view information must enter a password, after verification can display the content of the code how to achieve?
Setting a separate access password for some php pages, if the password is incorrect, you cannot view the content, which is equivalent to an encryption of the page.
Effect screenshot
How to use and steps
Create a new MkEncrypt.php file in your root directory or in your sibling directory.
Add the following code to MkEncrypt.php:
<? php if(! defined('MK_ENCRYPT_SALT')) define('MK_ENCRYPT_SALT', 'Kgs$JC! V'); /** * Set access password * @param $password Access password * @param $pageid Unique page ID value, Used to distinguish between different encrypted pages of the same website */ 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 password verification is correct if($postpwd == $password) {// Submitted password is correct 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> This page has been encrypted </title> <style type="text/css"> *{font-family:"Microsoft Yahei", 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 L590.547 895.036 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 55.959 0 0 0 0 0 47.525-85.534zm-470.619-85.172a48.008 48.008 01 1-96.015 0v-1.567a48.008 48.008 01 1 96.015 0v1.567zm0-175.345a48.008 48.008 01 1-96.015 0V379.362a48.008 48.008 01 1 96.015 0v255.157z" fill="#FF9800"/> </svg> <form action="" method="post" class="mk-side-form"> <h2 class="pw-tip"> This page is encrypted </h2> <input type="password" name="pagepwd" placeholder=" Please enter the access password to view "required><button type="submit"> Access </button> <?php if($postpwd): ?> <p id="pw-error"> Incorrect password ~</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=" Go back to home page ">- Go back to home page - </a> </div> </body> </html> <? php exit(); }
Call the following code from the page you want to encrypt
<? php require_once('MkEncrypt.php'); MkEncrypt('123456'); ? >
MkEncrypt(‘123456’); 123456 in parentheses is changed to the password you need to set.
The password is correct to enter the page, the cookie value will be saved after entering, and the next time you log in, you do not need to enter again, as long as the PHP program is supported by this code.
Method supplement
In addition to the above methods, Xiaobian also sorted out some PHP methods for page encryption, I hope to help you
Setting a separate access password for some php pages, if the password is incorrect, you cannot view the content, which is equivalent to an encryption of the page. Just include the following php file at the very beginning where you need to set a separate access password.
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 // All need to output a second password to open the page, just need to include this php file can be $url = ' http://' .$_SERVER['SERVER_NAME'] . & # 39; : & # 39; .$_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 the & # 39;) { $_SESSION['recheck'] = 1; header(' location:' .$url); } if(! isset($_SESSION['recheck'] )){ exit(' <div id="divcss"> <form method="post"> Please enter a separate access password: <input type="password" name="password" /> <input type="submit" value=" OK "/> (Password: 123456) </form> </div> ') ; }? > <div id="footer"><a href="? close=yes&url=<? php echo $url? > external "rel =" nofollow "> < font color =" # FFFFFF "> security to exit this page < / font > < / a > < / div > < / body > < / HTML >
The php file can be included in the page that needs to be set up to access the independent password, so that you can ensure that only after entering the correct access password can access the specified page; You can also slightly modify the package into a function directly inserted into the top of the page that needs to set the access password, so that you can set a different access password for each page!
<? php include(‘recheck.php'); ? >
To this article about how to achieve PHP to set independent access password to the page is introduced to this, more related PHP page set access password content please search the script home previous articles or continue to browse the following related articles hope that you will support the script home in the future!
- PHP implements the effect of encrypting text files and restricting access to specific pages
- Common PHP encryption and decryption function share
- PHP code encryption and extended decryption practice
- Six examples of php encryption and decryption methods
- PHP generates a random password method summary
- PHP password encryption in several ways
Related article
-
In-depth study of php regular expressions
This article is a detailed analysis of php regular expressions, the need for a friend's reference2013-06-06 -
Zend studio file annotation template setting method
This article mainly introduces examples of creating templates for file comments, function comments, code blocks, etc., in Zend studio2013-09-09 -
Use the PHP reflection mechanism to construct the sql statement "CREATE TABLE"
Today Xiaobian for everyone to share an article about the use of PHP reflection mechanism to construct "CREATE TABLE" sql statement, Xiaobian feel that the content is very good, now share with you, has a good reference value, need friends to follow Xiaobian to see2019-03-03 -
Wechat uses PHP to create a custom menu method
This article mainly introduces php to create wechat public platform account custom menu class method, very practical skills, interested partners can refer to it2016-08-08 -
PHP cache mechanism Output Control details
This article mainly introduces the PHP caching mechanism Output Control, the need for friends can refer to the next2014-07-07 -
A method in php that uses explode to find out if a character exists
To make it easier to type and reduce the amount of text boxes that appear when decorating the edit module, explode and build a set of data by typing two or three different things in one box, then using special characters to separate them.2011-07-07 -
A brief discussion on the object Pool pattern of PHP design pattern
The object pool pattern is a creative design pattern that prepares a set of initialized "pools" of objects in advance rather than creating or destroying them on demand. The object pool client requests an object from the object pool and then performs an operation using the returned object. When the client is finished, it returns this particular type of factory object to the object pool instead of destroying it.2021-05-05 -
Laravel uses Ali Cloud OSS Composer package to share
This article mainly introduces the use of Ali Cloud OSS Composer package sharing in Laravel, this article explains its use, the file download please go to the github address in the file, the need for friends can refer to2015-02-02 -
PHP fastcgi mode upload large files (about 300 + K) error
When uploading the image, about 300K, the result reported a server error, fastcgi default request size is 131072, so in the apache configuration to add MaxRequestLen configuration is good2014-09-09 -
php XPath implements code for finding and modifying XML files
php XPath to XML file search and modify the implementation code, the need for friends can refer to the next.2011-07-07
Latest comments