By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Prompting for a password only once per session

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <SCRIPT language="JavaScript">
  3. <!--hide
  4.     var password = prompt("Please enter the password","");
  5.     if (password == "correct") { 
  6.         alert("You got it!");
  7.     }
  8.  
  9.     else {
  10.         alert("Sorry, that's not right.");
  11.         window.location="http://google.com";
  12.     }
  13. //-->
  14. </SCRIPT>
  15.  
Apr 22 '12 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Apr 22 '12 #2

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1. var key_value = "myTestCookie=true"; 
  2. var foundCookie = 0; 
  3.  
  4. // Get all the cookies from this site and store in an array 
  5. var cookieArray = document.cookie.split(';'); 
  6.  
  7.     // Walk through the array 
  8.     for(var i=0;i < cookieArray.length;i++) 
  9.         { 
  10.                var checkCookie = cookieArray[i]; 
  11.         // Remove any leading spaces 
  12.                while (checkCookie.charAt(0)==' ') 
  13.                { 
  14.                  checkCookie = checkCookie.substring(1,checkCookie.length); 
  15.                } 
  16.  
  17.         // Look for cookie set by key_value 
  18.                 if (checkCookie.indexOf(key_value) == 0) 
  19.                { 
  20.                   alert("Found Cookie "); 
  21.             // The cookie was found so set the variable 
  22.                    foundCookie = 1; 
  23.                } 
  24.     } 
  25.     // Check if a cookie has been found 
  26.     if ( foundCookie == 0) 
  27.     { 
  28.         // The key_value cookie was not found so set it now 
  29.         document.cookie = key_value; 
  30.         alert("Setting Cookie"); 
  31.     }  
  32.  
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 :)
Apr 22 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
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:
Expand|Select|Wrap|Line Numbers
  1. if (!sessionStorage.getItem("loggedin")) {
  2.     window.location.href = "login.html";
  3. }
Expand|Select|Wrap|Line Numbers
  1. // login.html
  2. var pw = prompt("Please enter password:");
  3. if ("password" == pw) {
  4.     sessionStorage.setItem("loggedin", 1);
  5. }
Apr 22 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.