How to Create Password Protected Webpage Using PHP

Do you want to have a hidden page on your website which only people who you give the password to can access it? Most probably you do as we all have something that we want to hide from prying eyes, that is were this PHP password protect page will come in useful. You may also like How to generate random password using PHP and MySQL.

HTML Code

<html>
    <head>
    </head>
    <body>
    <?php
    if(@$_SESSION['password']=="123456")
    {
     ?>
     <h1>How to Create Password Protected Webpage Using PHP</h1>
     <form method="post" action="" id="logout_form">
         <input type="submit" name="page_logout" value="LOGOUT">
         </form>
     <?php
    }
    else
    {
     ?>
     <form method="post" action="" id="login_form">
        <h1>LOGIN TO PROCEED</h1>
        <input type="password" name="pass" placeholder="*******">
        <input type="submit" name="submit_pass" value="DO SUBMIT">
        <p>"Password : 12345"</p>
        <p><font style="color:red;"><?php echo $error;?></font></p>
     </form>
     <?php	
    }
    ?>
    </body>
</html>

PHP Code

<?php
session_start();
$error = '';
if (isset($_POST['submit_pass']) && $_POST['pass']) {
    $pass = $_POST['pass'];
    if ($pass == "123456") {
        $_SESSION['password'] = $pass;
    } else {
        $error = "Incorrect Pssword";
    }
}

if (isset($_POST['page_logout'])) {
    unset($_SESSION['password']);
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *