How do I make my password prompt only show up once when I open up my website instead of every time I reload the page or revisit it

Viewed 100

Can I set a time limit (for example in 8 hours you have to retype the password)? Here's my very simple javascript:

<SCRIPT language="JavaScript">
    <!--hide
    var password;
    var pass1="x10Kz4iz4ZvEB2wgUBA5otc1";
    var pass2="x10Kz4iz4ZvEB2wgUBA5otc1";
    var pass3="x10Kz4iz4ZvEB2wgUBA5otc1"
    
    password=prompt('Please enter your password to view this page!',' ');
    
    if (password==pass1 || password==pass2 || password==pass3)
      alert('Password Correct! Click OK to enter!');
    else
       {
        alert('Uh oh try again');
        window.location="#";
        }  
    //-->
</SCRIPT>

I have this under the head tag of my HTML text. Thank you :)

1 Answers

This is not really a secure way of protecting your website because anyone can look at the source code of a page and see the Javascript along with the passwords you wrote there.

However, for learning purposes, you might want to learn about setting and getting cookies (How do I create and read a value from cookie?). You can store the time the user logged in, and next time check if it has been eight hours (just compare the time and date of last login to current login time and date).

Also, to get the current time, you should read up on the Javascript Date() function.

Related