PHP Program to Protect Part Or Content Of A Web Page

There are some ways to make a particular content of a webpage password protected with PHP.
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 will learn a secure server-side program for PHP-based websites 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

Below are the steps to perform our task:

  1. Give a password box to the user so that the user can input a password to see the protected content.
  2. Then pass the value of the password box using the post method with an HTML form. ( I prefer this method because it will not show the password in the URL as query string)
  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 password matches then display the Protected Content using the PHP echo.

Protect website from SQL Injection in PHP

 

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

Look at the content below:

<!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>

Below is given 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 *