in php password protected page, secure Password protect your data content with Web Page Password Protect by just including single line of PHP source code to your web page source. full source code will present user with secure password entry form, and will not let any type of the visitor see your private all the data content without accessing a secure password.
Create Password Protected Webpage Using PHP, HTML And CSS
Contents
Bellow simple Example to learn step by step Free PHP Scripts :: Web Page Password Protect.
Also Read : Password Protecting Website running on NGinx serve PHP-FPM
Step 1. Create a PHP file and define markup
I simply Create a PHP file as well save it with a name password.php
<?php session_start(); if(isset($_POST['submit_pass']) && $_POST['pass']) { $pass=$_POST['pass']; if($pass=="98256") { $_SESSION['password']=$pass; } else { $error="sorry dear, your Incorrect Pssword"; } } if(isset($_POST['home_sign_out'])) { unset($_SESSION['password']); } ?> <html> <head> <link rel="stylesheet" type="text/css" href="password_style.css"> </head> <body> <div id="wrapper"> <?php if($_SESSION['password']=="98256") { ?> <h1>Create Password Protected Webpage Using PHP, HTML And CSS - www.pakainfo.com</h1> <form method="post" action="" id="logout_form"> <input type="submit" name="home_sign_out" value="LOGOUT"> </form> <?php } else { ?> <form method="post" action="" id="signin_frm"> <h1>Sign In TO PROCEED</h1> <input type="password" name="pass" placeholder="************"> <input type="submit" name="submit_pass" value="DO SUBMIT"> <p>"Password : 98256"</p> <p><font style="color:red;"><?php echo $error;?></font></p> </form> <?php } ?> </div> </body> </html>
In this phase i first of all check if user sign in or not by verifyed then session variable if the user is not sign in i will show signin form and if user is sign in i showing webpage data content with sign out button.i use main two isset() condition to do signin or sign out.In simple main condition i simply read the user password as well as check if the set of the password is ‘98256’ if yes i update the password in session variable using php as well as then show the webpage.In onther condition i simply unset the session variable using php which stores/saved password value.You may also such as a simple http secure authentication using PHP source code.
Step 2. Create a CSS file and define styling
i create a CSS file and save it with a name password_style.css
body { margin:0 auto; padding:0px; text-align:center; width:100%; font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif; background-color:#8A4B08; } #wrapper { margin:0 auto; padding:0px; text-align:center; width:995px; } #wrapper h1 { margin-top:50px; font-size:45px; color:white; } #wrapper p { font-size:16px; } #logout_form input[type="submit"] { width:250px; margin-top:10px; height:40px; font-size:16px; background:none; border:2px solid white; color:white; } #signin_frm { margin-top:200px; background-color:white; width:350px; margin-left:310px; padding:20px; box-sizing:border-box; box-shadow:0px 0px 10px 0px #3B240B; } #signin_frm h1 { margin:0px; font-size:25px; color:#8A4B08; } #signin_frm input[type="password"] { width:250px; margin-top:10px; height:40px; padding-left:10px; font-size:16px; } #signin_frm input[type="submit"] { width:250px; margin-top:10px; height:40px; font-size:16px; background-color:#8A4B08; border:none; box-shadow:0px 4px 0px 0px #61380B; color:white; border-radius:3px; } #signin_frm p { margin:0px; margin-top:15px; color:#8A4B08; font-size:17px; font-weight:bold; }
Easy way to password-protect php page
in this way simply you can Create two files:
- protect-this.php
- login.php:
protect-this.php
<?php $password = '[email protected]'; if (empty($_COOKIE['password']) || $_COOKIE['password'] !== $password) { header('Location: login.php'); exit; } ?>
login.php:
<?php $password = '[email protected]'; $redirect_after_login = 'index.php'; $remember_password = strtotime('+30 days'); // 30 days if (isset($_POST['password']) && $_POST['password'] == $password) { setcookie("password", $password, $remember_password); header('Location: ' . $redirect_after_login); exit; } ?> <!DOCTYPE html> <html> <head> <title>Password protected - www.pakainfo.com</title> </head> <body> <div style="text-align:center;margin-top:50px;"> You must enter the password to view this content. <form method="POST"> <input type="text" name="password"> </form> </div> </body> </html>
Note: Then you can simply need to protect-this.php on the TOP of the main files you want to protect:
Password protect this content
require_once('protect-this.php');
I hope you get an idea about php password protected page.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donβt forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.