Prompting for a password only once per session

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pralaya
    New Member
    • Apr 2012
    • 2

    Prompting for a password only once per session

    hello!

    I have a blog I would love to put a password on. I've put a simple one on it but it asks me to input a password every reload which is quite annoying.

    Is there any way to make it so that it prompts only once per visit?

    I've tried to do this myself but I have limited knowledge of how Javascript works and I've gotten nowhere. Also, it's not supposed to be a super serious page or anything, I'm aware you can just view source to nab the password. That's not too much of a concern, just want to deter some traffic is all.

    Note: it must be Javascript and I can't do a server side password or use php. :(

    What I have:

    Code:
        
    <SCRIPT language="JavaScript">
    <!--hide
        var password = prompt("Please enter the password","");
        if (password == "correct") { 
            alert("You got it!");
        }
    
        else {
            alert("Sorry, that's not right.");
            window.location="http://google.com";
        }
    //-->
    </SCRIPT>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that’s correct behaviour. essentially, you don’t save the password anywhere and JS forgets everything between reloads.

    besides that it’s pointless to ask for a password when you can get it by looking at the page source.

    I strongly recommend to make password verification the job of a server-side technology.

    Comment

    • pralaya
      New Member
      • Apr 2012
      • 2

      #3
      I cannot do a server-side password as I do not have access to the server. I have a single page blog which I want to make a little more private than it is currently.

      Is it possible to use what I have (above password script) and incorporate it into this somehow? Again, I'd do it myself, but I'm not that familiar with it.

      Code:
      var key_value = "myTestCookie=true"; 
      var foundCookie = 0; 
      
      // Get all the cookies from this site and store in an array 
      var cookieArray = document.cookie.split(';'); 
      
          // Walk through the array 
          for(var i=0;i < cookieArray.length;i++) 
              { 
                     var checkCookie = cookieArray[i]; 
              // Remove any leading spaces 
                     while (checkCookie.charAt(0)==' ') 
                     { 
                       checkCookie = checkCookie.substring(1,checkCookie.length); 
                     } 
              
              // Look for cookie set by key_value 
                      if (checkCookie.indexOf(key_value) == 0) 
                     { 
                        alert("Found Cookie "); 
                  // The cookie was found so set the variable 
                         foundCookie = 1; 
                     } 
          } 
          // Check if a cookie has been found 
          if ( foundCookie == 0) 
          { 
              // The key_value cookie was not found so set it now 
              document.cookie = key_value; 
              alert("Setting Cookie"); 
          }
      If this is impossible to do with just a quick Javascript code just say so and I'll try to settle with what I have. I'm sorry that I'm being so fussy but thank you for replying :)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I cannot do a server-side password as I do not have access to the server. I have a single page blog which I want to make a little more private than it is currently.
        the point is that JavaScript cannot add privacy. all I need to do is hit Ctrl + (Shift) + U, read the passwort and enter it. I can even bypass it completely by simply turning off JavaScript.

        back to the code, if you don’t need to support the older browsers, you can go with the Web Storage (similar to cookies, only much better)

        in that case the code would become much easier:
        Code:
        if (!sessionStorage.getItem("loggedin")) {
            window.location.href = "login.html";
        }
        Code:
        // login.html
        var pw = prompt("Please enter password:");
        if ("password" == pw) {
            sessionStorage.setItem("loggedin", 1);
        }

        Comment

        Working...