473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:

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
3 2284
Dormilich
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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

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

Similar topics

4
by: dr. zoidberg | last post by:
Hello, I'm trying to create login system. I need some advices. Should I put Username and Password in session, and then check database for correct combination on every page, or should I just put...
3
by: Patrick.O.Ige | last post by:
I have a problem with the code below:- When i use a username for example dog for the first time it works but later when i use cat for example it keeps showing Hello:- Dog.. It keeps DOG in the...
2
by: Rheadore | last post by:
Hi , Please give me any links/examples to create a page which can store my password in cache, call that cache when I enter my userid, automatically insert my password in the text box later. ...
1
by: Lee | last post by:
Hi, I have a Web application with Windows authentication. I have set a timeout of 20 minutes. Once the application is opened from the client with the local system login, IE prompts for user...
7
by: Leon | last post by:
Is it a good idea to load a user username/email and password into session state if you will be validating information against those credential through-out the use of the web application? or is it...
22
by: K. A. | last post by:
I have two servers at work, 'A' for testing and development, and server 'B' for production. On server A, I wrote a PHP test code to login users then direct them to a personalized page. This is...
0
by: rao123 | last post by:
Hi, I want to execute some set of Postgres routines like psql,pg_dump,pg_restore in a single MSDos batch file. I want to prompt user only once for password and use that in entire batch file with...
7
by: gowthamkg | last post by:
For the following code when ever i open the this page the text box has / i ahve set the value of text box with session variables whic is exuted only when submit button is clicked....Pls Help me.......
3
by: bollweevil | last post by:
Hello Everyone, I do Django web development on my Mac at home, and then I rsync the files with the Ubuntu web server. I want to write one single bash shell script that rsyncs the files and...
1
by: lisa007 | last post by:
i have a form if user forgot password it send the new password to the user email but now i'm trying to is to also send when user register but is not wrking this is the forgot password which works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.