user3132508
user3132508

Reputation: 93

How to create a password protected website?

I am in the middle of creating my website and I need to create password protected JavaScript code to access it.

Do I need to change my CSS as well, or it will be only in my HTML code?

Upvotes: -1

Views: 7269

Answers (4)

Shaurya
Shaurya

Reputation: 1

You can just put all this in an echo statement in php to just hide the code

The html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the radio button to toggle between password visibility:</p>
    
    Password: 
    <input type='text' value='' id='myInput'><br><br>
    <input type='checkbox' onclick='myFunction()'>Show results
    
    <p id='demo' style='display:none; color: black;'>demo</p>

And now the javascript it is quite simple

    <script>
    function myFunction() {
      var x = document.getElementById('myInput');
      var y = document.getElementById('demo');
      if (x.value === '45') {
        y.style.display = 'block';
      } else {
        y.style.display = 'none';
      }
    }
    </script>

and close the html

    </body>
    </html>

done that's it just try it and enter the password 45

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

Reputation: 206525

Front-end JS is a bad protection

// IMPORTANT: This is a JS-for-fun - and a BAD example how to secure your content:
var password = "demo"; // because ANYONE CAN SEE THIS IN VIEW SOURCE!


// Repeatedly prompt for user password until success:
(function promptPass() {

  var psw = prompt("Enter your Password");

  while (psw !== password) {
    alert("Incorrect Password");
    return promptPass();
  }

}());


alert('WELCOME');
// or show you page content

To properly protect your page:
a secured connection with your server, and a server-side language is the way to go.

Upvotes: 5

Sunny
Sunny

Reputation: 119

You can not use javascript or css to create Password Protected website.

You have to use server-side scripting like php, asp.net, jsp or user htaccess password protection

Upvotes: 1

Venge
Venge

Reputation: 2437

It's impossible to have any kind of meaningful password protection using pure JS since the client can just change the code of your page to skip the password protection entirely. You'll need to either use HTTP basic access authentication or use some kind of server-side scripting language.

Upvotes: 5

Related Questions