PHP Program to Protect Part Or Content Of A Web Page

By Saruque Ahamed Mollick

There are many ways to make a particular content of a webpage password protected.
Even we can use JavaScript to do that. But that is not secure enough because that is a client-side procedure.
So in this post, we gonna learn a secure server-side program to protect particular content or contents of a webpage.

How To Generate Random Password In PHP?

 

Steps to make web content password protected in PHP

  1. Give a password box to the user so that user can input password to see the protected content
  2. Then pass the value of the password box using post method. ( I prefer this method because it will not show the password in the URL)
  3. Store the value in a PHP variable.
  4. Now add an “if else” condition to compare the password entered by the user with the original password.
  5. If the passwords are same then echo the Protected Content.

Protect website from SQL Injection in PHP

 



PHP program to protect particular HTML content on a webpage by password

<!DOCTYPE html>
<html>
<head>
  <title>Pw protection</title>

</head>
<body>
  <h3>I'm not password protected</h3>
  <h2>I'm also not password protected</h2>
  <h2>below content is password protected, Please enter the password to see the below content</h2>
  <form method="post">
    <input type="password" name="pw">
    <input type="submit" name="submit" value="Submit password">
  </form>
  <?php
  if (isset($_POST['submit'])) {
    $example = $_POST['pw'];
    if($example=="rightpassword"){
    	echo "<h3>You are eligible to see the content now and this the content</h3>";
    }
    else
    {
    	echo "Wrong Password Or You are not eligible to see the content";
    }
  }
  
?>


</body>
</html>

Here is the webpage screenshot

Password protected web content

If the user entered the right password webpage will be like this

PHP content protection of a webpage by password

 

Leave a Reply

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